DEV: Consolidate Redis evalsha logic into DiscourseRedis::EvalHelper (#15957)

This commit is contained in:
David Taylor
2022-02-15 16:06:12 +00:00
committed by GitHub
parent dd5373cc4c
commit 11c93342dc
5 changed files with 79 additions and 49 deletions

View File

@ -49,6 +49,12 @@ module Middleware
ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING"
DISCOURSE_RENDER = "HTTP_DISCOURSE_RENDER"
REDIS_STORE_SCRIPT = DiscourseRedis::EvalHelper.new <<~LUA
local current = redis.call("incr", KEYS[1])
redis.call("expire",KEYS[1],ARGV[1])
return current
LUA
def initialize(env, request = nil)
@env = env
@request = request || Rack::Request.new(@env)
@ -259,11 +265,7 @@ module Middleware
if status == 200 && cache_duration
if GlobalSetting.anon_cache_store_threshold > 1
count = Discourse.redis.eval(<<~REDIS, [cache_key_count], [cache_duration])
local current = redis.call("incr", KEYS[1])
redis.call("expire",KEYS[1],ARGV[1])
return current
REDIS
count = REDIS_STORE_SCRIPT.eval(Discourse.redis, [cache_key_count], [cache_duration])
# technically lua will cast for us, but might as well be
# prudent here, hence the to_i