added a clear cache rake task, removed fake redis for mockredis, slight
refactor of the redis_cache class
This commit is contained in:
parent
0d00265392
commit
f4bfa22768
4 changed files with 44 additions and 15 deletions
|
|
@ -81,10 +81,18 @@ class RedisCache
|
|||
::AspectStream::TYPES_OF_POST_IN_STREAM
|
||||
end
|
||||
|
||||
# Instantiate a redis connection
|
||||
#
|
||||
# @return [Redis]
|
||||
def self.redis_connection
|
||||
Redis.new(:host => RedisCache.redis_host, :port => RedisCache.redis_port)
|
||||
end
|
||||
|
||||
protected
|
||||
# @see .redis_connection
|
||||
# @return [Redis]
|
||||
def redis
|
||||
@redis ||= Redis.new(:host => RedisCache.redis_host, :port => RedisCache.redis_port)
|
||||
@redis ||= RedisCache.redis_connection
|
||||
end
|
||||
|
||||
def self.redis_host
|
||||
|
|
@ -95,8 +103,13 @@ class RedisCache
|
|||
(AppConfig[:redis_port].blank?) ? nil : AppConfig[:redis_port]
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def self.cache_prefix
|
||||
"cache_stream"
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def set_key
|
||||
@set_key ||= "cache_stream_#{@user.id}_#{@order_field}"
|
||||
@set_key ||= "#{RedisCache.cache_prefix}_#{@user.id}_#{@order_field}"
|
||||
end
|
||||
end
|
||||
|
|
|
|||
17
lib/tasks/cache.rake
Normal file
17
lib/tasks/cache.rake
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
namespace :cache do
|
||||
|
||||
desc "Clear all caches"
|
||||
task :clear => :environment do
|
||||
if RedisCache.configured?
|
||||
redis = Redis.redis_connection
|
||||
redis.keys do |k|
|
||||
if k.match(/^#{RedisCache.cache_prefix}/).present?
|
||||
redis.del(k)
|
||||
end
|
||||
end
|
||||
else
|
||||
puts "Redis Cache is not configured"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -161,6 +161,14 @@ describe RedisCache do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#set_key" do
|
||||
it 'uses the correct prefix and order' do
|
||||
user = @cache.instance_variable_get(:@user)
|
||||
order_field = @cache.instance_variable_get(:@order_field)
|
||||
@cache.send(:set_key).should == "#{RedisCache.cache_prefix}_#{user.id}_#{order_field}"
|
||||
end
|
||||
end
|
||||
|
||||
describe '.cache_setup?' do
|
||||
it 'returns true if configuration is properly set' do
|
||||
AppConfig[:redis_cache] = true
|
||||
|
|
|
|||
|
|
@ -1,20 +1,11 @@
|
|||
module Diaspora::WebSocket
|
||||
def self.redis
|
||||
FakeRedis.new
|
||||
MockRedis.new
|
||||
end
|
||||
end
|
||||
|
||||
class FakeRedis
|
||||
def rpop(*args)
|
||||
true
|
||||
end
|
||||
def llen(*args)
|
||||
true
|
||||
end
|
||||
def lpush(*args)
|
||||
true
|
||||
end
|
||||
def sismember(*args)
|
||||
false
|
||||
class RedisCache
|
||||
def self.redis_connection
|
||||
MockRedis.new
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue