mirror of
https://github.com/discourse/discourse.git
synced 2024-12-05 12:03:39 +08:00
b6bf95e741
correct group permission leaks add Discourse.cache for richer caching support
56 lines
957 B
Ruby
56 lines
957 B
Ruby
# Standard Rails.cache is lacking support for this interface, possibly yank all in from redis:cache and start using this instead
|
|
#
|
|
|
|
class Cache
|
|
def initialize(redis=nil)
|
|
@redis = redis
|
|
end
|
|
|
|
def redis
|
|
@redis || $redis
|
|
end
|
|
|
|
def fetch(key, options={})
|
|
result = redis.get key
|
|
if result.nil?
|
|
if expiry = options[:expires_in]
|
|
if block_given?
|
|
result = yield
|
|
redis.setex(key, expiry, result)
|
|
end
|
|
else
|
|
if block_given?
|
|
result = yield
|
|
redis.set(key, result)
|
|
end
|
|
end
|
|
end
|
|
|
|
if family = family_key(options[:family])
|
|
redis.sadd(family, key)
|
|
end
|
|
|
|
result
|
|
end
|
|
|
|
def delete(key)
|
|
redis.del(key)
|
|
end
|
|
|
|
def delete_by_family(key)
|
|
k = family_key(key)
|
|
redis.smembers(k).each do |member|
|
|
delete(member)
|
|
end
|
|
redis.del(k)
|
|
end
|
|
|
|
private
|
|
|
|
def family_key(name)
|
|
if name
|
|
"FAMILY_#{name}"
|
|
end
|
|
end
|
|
end
|