mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 00:19:36 +08:00
speed up startup (avoid loading some gems on startup)
correct group permission leaks add Discourse.cache for richer caching support
This commit is contained in:
55
lib/cache.rb
Normal file
55
lib/cache.rb
Normal file
@ -0,0 +1,55 @@
|
||||
# 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
|
Reference in New Issue
Block a user