Class: SimpleCache::RedisStore
- Inherits:
-
Object
- Object
- SimpleCache::RedisStore
- Defined in:
- lib/simple_cache/redis_store.rb
Constant Summary collapse
- Marshal =
::SimpleCache::Marshal
Instance Method Summary collapse
- #clear ⇒ Object
- #fetch(key, &block) ⇒ Object
-
#initialize(redis) ⇒ RedisStore
constructor
A new instance of RedisStore.
- #store(key, value, max_age = nil) ⇒ Object
Constructor Details
#initialize(redis) ⇒ RedisStore
Returns a new instance of RedisStore.
7 8 9 10 11 12 13 14 |
# File 'lib/simple_cache/redis_store.rb', line 7 def initialize(redis) case redis when Redis, Redis::Namespace @redis = redis else @redis = Redis.connect(:url => redis) end end |
Instance Method Details
#clear ⇒ Object
16 17 18 |
# File 'lib/simple_cache/redis_store.rb', line 16 def clear @redis.flushdb end |
#fetch(key, &block) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/simple_cache/redis_store.rb', line 20 def fetch(key, &block) if value = @redis.get(Marshal.uid(key)) Marshal.unmarshal(value) elsif block_given? yield self, key end end |
#store(key, value, max_age = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/simple_cache/redis_store.rb', line 28 def store(key, value, max_age = nil) cache_id = Marshal.uid(key) if value @redis.set cache_id, Marshal.marshal(value) @redis.expire cache_id, max_age if max_age else @redis.del cache_id end value end |