2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
# Discourse specific cache, enforces 1 day expiry by default
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
# This is a bottom up implementation of ActiveSupport::Cache::Store
|
|
|
|
# this allows us to cleanly implement without using cache entries and version
|
|
|
|
# support which we do not use, in tern this makes the cache as fast as simply
|
2019-12-03 04:05:53 -05:00
|
|
|
# using `Discourse.redis.setex` with a more convenient API
|
2019-11-27 00:11:49 -05:00
|
|
|
#
|
|
|
|
# It only implements a subset of ActiveSupport::Cache::Store as we make no use
|
|
|
|
# of large parts of the interface.
|
|
|
|
#
|
|
|
|
# An additional advantage of this class is that all methods have named params
|
|
|
|
# Rails tends to use options hash for lots of stuff due to legacy reasons
|
|
|
|
# this makes it harder to reason about the API
|
|
|
|
|
|
|
|
class Cache
|
2014-01-06 00:50:04 -05:00
|
|
|
|
2015-02-19 00:58:05 -05:00
|
|
|
# nothing is cached for longer than 1 day EVER
|
|
|
|
# there is no reason to have data older than this clogging redis
|
|
|
|
# it is dangerous cause if we rename keys we will be stuck with
|
|
|
|
# pointless data
|
|
|
|
MAX_CACHE_AGE = 1.day unless defined? MAX_CACHE_AGE
|
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
# we don't need this feature, 1 day expiry is enough
|
|
|
|
# it makes lookups a tad cheaper
|
|
|
|
def self.supports_cache_versioning?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(namespace: "_CACHE")
|
|
|
|
@namespace = namespace
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def redis
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis
|
2014-01-06 00:50:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reconnect
|
|
|
|
redis.reconnect
|
|
|
|
end
|
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
def keys(pattern = "*")
|
2018-12-14 19:53:52 -05:00
|
|
|
redis.scan_each(match: "#{@namespace}:#{pattern}").to_a
|
2018-05-03 09:41:41 -04:00
|
|
|
end
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
def clear
|
2018-05-03 09:41:41 -04:00
|
|
|
keys.each do |k|
|
2015-02-19 00:58:05 -05:00
|
|
|
redis.del(k)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
2014-01-06 00:50:04 -05:00
|
|
|
end
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
def normalize_key(key)
|
2019-05-02 18:17:27 -04:00
|
|
|
"#{@namespace}:#{key}"
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
def exist?(name)
|
|
|
|
key = normalize_key(name)
|
|
|
|
redis.exists(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
# this removes a bunch of stuff we do not need like instrumentation and versioning
|
|
|
|
def read(name)
|
|
|
|
key = normalize_key(name)
|
|
|
|
read_entry(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(name, value, expires_in: nil)
|
|
|
|
write_entry(normalize_key(name), value, expires_in: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(name)
|
|
|
|
redis.del(normalize_key(name))
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(name, expires_in: nil, force: nil, &blk)
|
|
|
|
if block_given?
|
|
|
|
key = normalize_key(name)
|
|
|
|
raw = nil
|
|
|
|
|
|
|
|
if !force
|
|
|
|
raw = redis.get(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
if raw
|
|
|
|
begin
|
|
|
|
Marshal.load(raw)
|
|
|
|
rescue => e
|
|
|
|
log_first_exception(e)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
val = blk.call
|
|
|
|
write_entry(key, val, expires_in: expires_in)
|
|
|
|
val
|
|
|
|
end
|
|
|
|
elsif force
|
|
|
|
raise ArgumentError, "Missing block: Calling `Cache#fetch` with `force: true` requires a block."
|
|
|
|
else
|
|
|
|
read(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
protected
|
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
def log_first_exception(e)
|
|
|
|
if !defined? @logged_a_warning
|
|
|
|
@logged_a_warning = true
|
|
|
|
Discourse.warn_exception(e, "Corrupt cache... skipping entry for key #{key}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_entry(key)
|
2014-01-06 00:50:04 -05:00
|
|
|
if data = redis.get(key)
|
2019-11-27 00:11:49 -05:00
|
|
|
Marshal.load(data)
|
2014-01-06 00:50:04 -05:00
|
|
|
end
|
2019-11-27 00:11:49 -05:00
|
|
|
rescue => e
|
|
|
|
# corrupt cache, this can happen if Marshal version
|
|
|
|
# changes. Log it once so we can tell it is happening.
|
|
|
|
# should not happen under any normal circumstances, but we
|
|
|
|
# do not want to flood logs
|
|
|
|
log_first_exception(e)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
def write_entry(key, value, expires_in: nil)
|
|
|
|
dumped = Marshal.dump(value)
|
|
|
|
expiry = expires_in || MAX_CACHE_AGE
|
2015-02-19 00:58:05 -05:00
|
|
|
redis.setex(key, expiry, dumped)
|
2014-01-06 00:50:04 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|