mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
BUGFIX: redis-rails has always been a problem child
implemented an ActiveSupport::Cache::Store for our internal use. * allows for expire by family * works correctly in multisite * namespaced correctly Removed redis-rails from the project, no longer needed
This commit is contained in:
95
lib/cache.rb
95
lib/cache.rb
@ -1,55 +1,72 @@
|
||||
# Standard Rails.cache is lacking support for this interface, possibly yank all in from redis:cache and start using this instead
|
||||
#
|
||||
# Discourse specific cache supports expire by family missing from standard cache
|
||||
|
||||
class Cache
|
||||
def initialize(redis=nil)
|
||||
@redis = redis
|
||||
class Cache < ActiveSupport::Cache::Store
|
||||
|
||||
def initialize(opts = {})
|
||||
opts[:namespace] ||= "_CACHE_"
|
||||
super(opts)
|
||||
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)
|
||||
$redis
|
||||
end
|
||||
|
||||
def delete_by_family(key)
|
||||
k = family_key(key)
|
||||
k = family_key(key, options)
|
||||
redis.smembers(k).each do |member|
|
||||
delete(member)
|
||||
redis.del(member)
|
||||
end
|
||||
redis.del(k)
|
||||
end
|
||||
|
||||
private
|
||||
def reconnect
|
||||
redis.reconnect
|
||||
end
|
||||
|
||||
def family_key(name)
|
||||
if name
|
||||
"FAMILY_#{name}"
|
||||
def clear
|
||||
redis.keys.each do |k|
|
||||
redis.del(k) if k =~ /^_CACHE_:/
|
||||
end
|
||||
end
|
||||
|
||||
def namespaced_key(key, opts=nil)
|
||||
opts ||= options
|
||||
super(key,opts)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def read_entry(key, options)
|
||||
if data = redis.get(key)
|
||||
ActiveSupport::Cache::Entry.new data
|
||||
end
|
||||
end
|
||||
|
||||
def write_entry(key, entry, options)
|
||||
if expiry = options[:expires_in]
|
||||
redis.setex(key, expiry, entry.value)
|
||||
else
|
||||
redis.set(key, entry.value)
|
||||
end
|
||||
|
||||
if family = family_key(options[:family], options)
|
||||
redis.sadd(family, key)
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def delete_entry(key, options)
|
||||
redis.del key
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def family_key(name, options)
|
||||
if name
|
||||
key = namespaced_key(name, options)
|
||||
key << "FAMILY:#{name}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user