Class: Scat::Cache
- Inherits:
-
Object
- Object
- Scat::Cache
- Defined in:
- lib/scat/cache.rb
Overview
The session and global key => value cache wrapper.
Instance Method Summary collapse
-
#add(tag, value) ⇒ Object
Adds the given value to the cache tag set.
-
#datastore ⇒ Redis
The Redis cache data store.
-
#get(tag, key = nil) ⇒ String+
The matching value or values.
-
#get_all(tag) ⇒ <String>
The matching set.
-
#set(tag, value, key = nil) ⇒ Object
Sets the given value to the cache tag set as follows: * If the key is nil, then the cache entry is the tag.
Instance Method Details
#add(tag, value) ⇒ Object
Adds the given value to the cache tag set. The value is converted to a string, if necessary.
35 36 37 |
# File 'lib/scat/cache.rb', line 35 def add(tag, value) datastore.zadd(tag, 0, value) end |
#datastore ⇒ Redis
Returns the Redis cache data store.
8 9 10 |
# File 'lib/scat/cache.rb', line 8 def datastore @redis ||= discover end |
#get(tag, key = nil) ⇒ String+
Returns the matching value or values.
42 43 44 |
# File 'lib/scat/cache.rb', line 42 def get(tag, key=nil) key ? datastore.hget(tag, key) : datastore.get(tag) end |
#get_all(tag) ⇒ <String>
Returns the matching set.
48 49 50 |
# File 'lib/scat/cache.rb', line 48 def get_all(tag) datastore.zrange(tag, 0, -1) end |
#set(tag, value, key = nil) ⇒ Object
Sets the given value to the cache tag set as follows:
-
If the key is nil, then the cache entry is the tag.
-
Otherwise, the cache entry is the tag hash entry for the given key.
-
If the value is nil, then the entry is removed from the cache.
-
Otherwise, the value is converted to a string, if necessary, and the cache entry is set to the value.
22 23 24 25 26 27 28 |
# File 'lib/scat/cache.rb', line 22 def set(tag, value, key=nil) if value.nil? then key ? datastore.hdel(tag, key) : datastore.rem(tag) else key ? datastore.hset(tag, key, value) : datastore.set(tag, value) end end |