2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-05 08:59:27 +08:00

FIX: emoji cache could get corrupt

FEATURE: enforce 1 day expiry by default on discourse cache

remove family expiry concept as the implementation was fragile
This commit is contained in:
Sam 2015-02-19 16:58:05 +11:00
parent 8da38cda81
commit 103d42a9d9
3 changed files with 30 additions and 51 deletions

View file

@ -1,9 +1,15 @@
# Discourse specific cache supports expire by family missing from standard cache
# Discourse specific cache, enforces 1 day expiry
class Cache < ActiveSupport::Cache::Store
# 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
def initialize(opts = {})
opts[:namespace] ||= "_CACHE_"
@namespace = opts[:namespace] || "_CACHE_"
super(opts)
end
@ -11,27 +17,18 @@ class Cache < ActiveSupport::Cache::Store
$redis
end
def delete_by_family(key)
k = family_key(key, options)
redis.smembers(k).each do |member|
redis.del(member)
end
redis.del(k)
end
def reconnect
redis.reconnect
end
def clear
redis.keys.each do |k|
redis.del(k) if k =~ /^_CACHE_:/
redis.keys("#{@namespace}:*").each do |k|
redis.del(k)
end
end
def namespaced_key(key, opts=nil)
opts ||= options
super(key,opts)
"#{@namespace}:" << key
end
protected
@ -47,17 +44,8 @@ class Cache < ActiveSupport::Cache::Store
def write_entry(key, entry, options)
dumped = Marshal.dump(entry.value)
if expiry = options[:expires_in]
redis.setex(key, expiry, dumped)
else
redis.set(key, dumped)
end
if family = family_key(options[:family], options)
redis.sadd(family, key)
end
expiry = options[:expires_in] || MAX_CACHE_AGE
redis.setex(key, expiry, dumped)
true
end
@ -65,13 +53,4 @@ class Cache < ActiveSupport::Cache::Store
redis.del key
end
private
def family_key(name, options)
if name
key = namespaced_key(name, options)
key << "FAMILY:#{name}"
end
end
end