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
|
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)
|
|
|
|
|
2021-01-04 04:34:44 -05:00
|
|
|
attr_reader :namespace
|
|
|
|
|
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 { |k| redis.del(k) }
|
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)
|
2020-06-14 21:57:44 -04:00
|
|
|
redis.exists?(key)
|
2019-11-27 00:11:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# this removes a bunch of stuff we do not need like instrumentation and versioning
|
|
|
|
def read(name)
|
|
|
|
key = normalize_key(name)
|
2024-06-18 05:48:00 -04:00
|
|
|
read_entry(key).tap { |entry| break if entry == :__corrupt_cache__ }
|
2019-11-27 00:11:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def write(name, value, expires_in: nil)
|
2021-01-04 04:34:44 -05:00
|
|
|
write_entry(normalize_key(name), value, expires_in: expires_in)
|
2019-11-27 00:11:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(name)
|
|
|
|
redis.del(normalize_key(name))
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(name, expires_in: nil, force: nil, &blk)
|
2024-06-18 05:48:00 -04:00
|
|
|
if !block_given?
|
|
|
|
if force
|
|
|
|
raise ArgumentError,
|
|
|
|
"Missing block: Calling `Cache#fetch` with `force: true` requires a block."
|
2019-11-27 00:11:49 -05:00
|
|
|
end
|
2024-06-18 05:48:00 -04:00
|
|
|
return read(name)
|
2019-11-27 00:11:49 -05:00
|
|
|
end
|
2024-06-18 05:48:00 -04:00
|
|
|
|
|
|
|
key = normalize_key(name)
|
2024-07-30 03:08:12 -04:00
|
|
|
|
|
|
|
if !force
|
|
|
|
if raw = redis.get(key)
|
|
|
|
entry = decode_entry(raw, key)
|
|
|
|
return entry if entry != :__corrupt_cache__
|
|
|
|
end
|
|
|
|
end
|
2024-06-18 05:48:00 -04:00
|
|
|
|
|
|
|
val = blk.call
|
|
|
|
write_entry(key, val, expires_in: expires_in)
|
|
|
|
val
|
2019-11-27 00:11:49 -05:00
|
|
|
end
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
protected
|
|
|
|
|
2024-06-18 05:48:00 -04:00
|
|
|
def log_first_exception(e, key)
|
|
|
|
return if defined?(@logged_a_warning)
|
|
|
|
@logged_a_warning = true
|
|
|
|
Discourse.warn_exception(e, message: "Corrupt cache... skipping entry for key #{key}")
|
2019-11-27 00:11:49 -05:00
|
|
|
end
|
|
|
|
|
2024-07-30 03:08:12 -04:00
|
|
|
def decode_entry(raw, key)
|
|
|
|
Marshal.load(raw) # rubocop:disable Security/MarshalLoad
|
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
|
2024-06-18 05:48:00 -04:00
|
|
|
log_first_exception(e, key)
|
|
|
|
:__corrupt_cache__
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
2024-07-30 03:08:12 -04:00
|
|
|
def read_entry(key)
|
|
|
|
if data = redis.get(key)
|
|
|
|
decode_entry(data, key)
|
|
|
|
end
|
|
|
|
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
|