<commit_before>local redis_pool = require 'redis_pool'

local _M = {
  error = {
    db_connection_failed = 'DB connection failed'
  }
}

local function get_auth_hash_key(service_id, app_id)
  return 'auth:'..service_id..':'..app_id
end

local function get_report_hash_key(service_id, app_id)
  return 'report:'..service_id..':'..app_id
end

local SET_REPORT_KEYS = 'report_keys'

function _M.authorize(service_id, app_id, usage_method)
  local redis, ok = redis_pool.acquire()

  if not ok then
    return nil, false, _M.error.db_connection_failed
  end

  local auth_hash_key = get_auth_hash_key(service_id, app_id)
  local cached_auth, err = redis:hget(auth_hash_key, usage_method)

  redis_pool.release(redis)

  if err then
    return nil, false, _M.error.db_connection_failed
  end

  local auth
  if cached_auth == '0' then
    auth = false
  elseif cached_auth == '1' then
    auth = true
  end

  return auth, true
end

-- Returns true if the report succeeds, false otherwise.
function _M.report(service_id, app_id, usage_method, usage_val)
  local redis, ok = redis_pool.acquire()

  if not ok then
    return false
  end

  local report_hash_key = get_report_hash_key(service_id, app_id)

  -- Run the 2 Redis commands in a pipeline to save network round-trip time.
  -- If executing a command in between is problematic, we should use a
  -- transaction with multi instead, but that's not clear yet.
  redis:init_pipeline(2)
  redis:hincrby(report_hash_key, usage_method, usage_val)
  redis:sadd(SET_REPORT_KEYS, report_hash_key)
  local result, _ = redis:commit_pipeline()

  redis_pool.release(redis)

  return result ~= nil
end

return _M
<commit_msg>cache: do not return error reason in authorize()<commit_after>local redis_pool = require 'redis_pool'

local _M = { }

local function get_auth_hash_key(service_id, app_id)
  return 'auth:'..service_id..':'..app_id
end

local function get_report_hash_key(service_id, app_id)
  return 'report:'..service_id..':'..app_id
end

local SET_REPORT_KEYS = 'report_keys'

function _M.authorize(service_id, app_id, usage_method)
  local redis, ok = redis_pool.acquire()

  if not ok then
    return nil, false
  end

  local auth_hash_key = get_auth_hash_key(service_id, app_id)
  local cached_auth, err = redis:hget(auth_hash_key, usage_method)

  redis_pool.release(redis)

  if err then
    return nil, false
  end

  local auth
  if cached_auth == '0' then
    auth = false
  elseif cached_auth == '1' then
    auth = true
  end

  return auth, true
end

-- Returns true if the report succeeds, false otherwise.
function _M.report(service_id, app_id, usage_method, usage_val)
  local redis, ok = redis_pool.acquire()

  if not ok then
    return false
  end

  local report_hash_key = get_report_hash_key(service_id, app_id)

  -- Run the 2 Redis commands in a pipeline to save network round-trip time.
  -- If executing a command in between is problematic, we should use a
  -- transaction with multi instead, but that's not clear yet.
  redis:init_pipeline(2)
  redis:hincrby(report_hash_key, usage_method, usage_val)
  redis:sadd(SET_REPORT_KEYS, report_hash_key)
  local result, _ = redis:commit_pipeline()

  redis_pool.release(redis)

  return result ~= nil
end

return _M

{'fraction_non_alphanumeric': 0.08557306147667594, 'fraction_numerical': 0.002471424158171146, 'mean_word_length': 4.164274322169059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1288230', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1129, 'n_words': 320}