Class: App47Cache
- Inherits:
-
Object
- Object
- App47Cache
- Includes:
- Singleton
- Defined in:
- lib/models/app47_cache.rb
Overview
Public: Provide a common interface to redis cache
Examples
RedisCacheService.delete("key name")
# => the value of the key deleted
RedisCacheService.get("key name")
# => the value of the key
Class Method Summary collapse
-
.delete(key) ⇒ Object
Delete a key from cache.
-
.get(key) ⇒ Object
Get a key from cache.
-
.redis ⇒ Object
Return the redis connection.
-
.set(key, value, ttl = nil) ⇒ Object
Set a key with a value and TTL.
Class Method Details
.delete(key) ⇒ Object
Delete a key from cache
key - the key to delete
32 33 34 |
# File 'lib/models/app47_cache.rb', line 32 def delete(key) redis.del key end |
.get(key) ⇒ Object
Get a key from cache
key - the key to fetch, if key is not found, returns nil
41 42 43 44 45 46 47 48 |
# File 'lib/models/app47_cache.rb', line 41 def get(key) return nil if ENV['RACK_ENV'].eql?('staging') value = redis.get key value.nil? ? nil : Marshal.restore(value) rescue StandardError nil end |
.redis ⇒ Object
Return the redis connection
23 24 25 |
# File 'lib/models/app47_cache.rb', line 23 def redis @redis ||= Redis.new(RedisConfiguration.load(12)) end |
.set(key, value, ttl = nil) ⇒ Object
Set a key with a value and TTL
key - the key to delete value - the value to set ttl(optional) - optional time to live parameter, default is no ttl, live forever
Returns the value passed in
59 60 61 62 63 64 65 66 67 |
# File 'lib/models/app47_cache.rb', line 59 def set(key, value, ttl = nil) in_value = Marshal.dump value if ttl redis.setex key, ttl, in_value else redis.set key, in_value end value end |