Class: HatiConfig::Cache::RedisAdapter
- Inherits:
-
Object
- Object
- HatiConfig::Cache::RedisAdapter
- Defined in:
- lib/hati_config/cache.rb
Overview
RedisAdapter class provides Redis-based caching.
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Deletes a value from the cache.
-
#get(key) ⇒ Object?
Gets a value from the cache.
-
#initialize(options) ⇒ RedisAdapter
constructor
A new instance of RedisAdapter.
-
#set(key, value, ttl) ⇒ Object
Sets a value in the cache.
Constructor Details
#initialize(options) ⇒ RedisAdapter
Returns a new instance of RedisAdapter.
244 245 246 247 248 |
# File 'lib/hati_config/cache.rb', line 244 def initialize() @pool = ConnectionPool.new(size: 5, timeout: 5) do Redis.new() end end |
Instance Method Details
#delete(key) ⇒ Object
Deletes a value from the cache.
277 278 279 280 281 |
# File 'lib/hati_config/cache.rb', line 277 def delete(key) @pool.with do |redis| redis.del(key) end end |
#get(key) ⇒ Object?
Gets a value from the cache.
254 255 256 257 258 259 260 261 |
# File 'lib/hati_config/cache.rb', line 254 def get(key) @pool.with do |redis| value = redis.get(key) value ? Marshal.load(value) : nil end rescue TypeError, ArgumentError nil end |
#set(key, value, ttl) ⇒ Object
Sets a value in the cache.
268 269 270 271 272 |
# File 'lib/hati_config/cache.rb', line 268 def set(key, value, ttl) @pool.with do |redis| redis.setex(key, ttl, Marshal.dump(value)) end end |