2014-01-06 00:50:04 -05:00
|
|
|
# Discourse specific cache supports expire by family missing from standard cache
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
class Cache < ActiveSupport::Cache::Store
|
|
|
|
|
|
|
|
def initialize(opts = {})
|
|
|
|
opts[:namespace] ||= "_CACHE_"
|
|
|
|
super(opts)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def redis
|
2014-01-06 00:50:04 -05:00
|
|
|
$redis
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_by_family(key)
|
|
|
|
k = family_key(key, options)
|
|
|
|
redis.smembers(k).each do |member|
|
|
|
|
redis.del(member)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
2014-01-06 00:50:04 -05:00
|
|
|
redis.del(k)
|
|
|
|
end
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
def reconnect
|
|
|
|
redis.reconnect
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
redis.keys.each do |k|
|
|
|
|
redis.del(k) if k =~ /^_CACHE_:/
|
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
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
def namespaced_key(key, opts=nil)
|
|
|
|
opts ||= options
|
|
|
|
super(key,opts)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def read_entry(key, options)
|
|
|
|
if data = redis.get(key)
|
2014-01-07 01:36:47 -05:00
|
|
|
data = Marshal.load(data)
|
2014-01-06 00:50:04 -05:00
|
|
|
ActiveSupport::Cache::Entry.new data
|
|
|
|
end
|
2014-01-07 01:36:47 -05:00
|
|
|
rescue
|
|
|
|
# corrupt cache, fail silently for now, remove rescue later
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
def write_entry(key, entry, options)
|
2014-01-07 01:36:47 -05:00
|
|
|
dumped = Marshal.dump(entry.value)
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
if expiry = options[:expires_in]
|
2014-01-07 01:36:47 -05:00
|
|
|
redis.setex(key, expiry, dumped)
|
2014-01-06 00:50:04 -05:00
|
|
|
else
|
2014-01-07 01:36:47 -05:00
|
|
|
redis.set(key, dumped)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
2014-01-06 00:50:04 -05:00
|
|
|
|
|
|
|
if family = family_key(options[:family], options)
|
|
|
|
redis.sadd(family, key)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_entry(key, options)
|
|
|
|
redis.del key
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
def family_key(name, options)
|
2013-05-13 04:04:03 -04:00
|
|
|
if name
|
2014-01-06 00:50:04 -05:00
|
|
|
key = namespaced_key(name, options)
|
|
|
|
key << "FAMILY:#{name}"
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
end
|
2014-01-06 00:50:04 -05:00
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|