Class: HatiConfig::Cache::RedisAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/hati_config/cache.rb

Overview

RedisAdapter class provides Redis-based caching.

Instance Method Summary collapse

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(options)
  @pool = ConnectionPool.new(size: 5, timeout: 5) do
    Redis.new(options)
  end
end

Instance Method Details

#delete(key) ⇒ Object

Deletes a value from the cache.

Parameters:

  • key (String) —

    The cache key



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.

Parameters:

  • key (String) —

    The cache key

Returns:

  • (Object, nil) —

    The cached value or nil if not found



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.

Parameters:

  • key (String) —

    The cache key

  • value (Object) —

    The value to cache

  • ttl (Integer) —

    The TTL in seconds



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