Class: Twitter::Cache::Redis
- Inherits:
-
Object
- Object
- Twitter::Cache::Redis
- Includes:
- Helpers
- Defined in:
- lib/twitter/cache/redis.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #get(key, ttl: nil) ⇒ Object
-
#initialize(prefix = nil) ⇒ Redis
constructor
A new instance of Redis.
- #method_missing(name, *args) ⇒ Object
- #set(key, value, ttl: nil) ⇒ Object
Methods included from Helpers
#cache, #config, #friends_cache, #user_cache
Constructor Details
#initialize(prefix = nil) ⇒ Redis
Returns a new instance of Redis.
10 11 12 13 14 15 |
# File 'lib/twitter/cache/redis.rb', line 10 def initialize(prefix = nil) redis_url = ENV['REDIS_URL'] || config.redis redis = ::Redis.new(url: redis_url) namespace = [config.namespace, prefix].compact.join(':') @client = ::Redis::Namespace.new(namespace, redis: redis) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
17 18 19 |
# File 'lib/twitter/cache/redis.rb', line 17 def method_missing(name, *args) client.send(name, *args) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
8 9 10 |
# File 'lib/twitter/cache/redis.rb', line 8 def client @client end |
Instance Method Details
#get(key, ttl: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/twitter/cache/redis.rb', line 21 def get(key, ttl: nil) stored = client.get(key) return Oj.load(stored, symbolize_keys: true) if stored return nil unless block_given? value = yield set(key, value, ttl: ttl) value end |
#set(key, value, ttl: nil) ⇒ Object
31 32 33 34 |
# File 'lib/twitter/cache/redis.rb', line 31 def set(key, value, ttl: nil) client.set(key, Oj.dump(value)) client.expire(key, ttl) if ttl end |