Class: FaradayThrottler::RedisCache

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday_throttler/redis_cache.rb

Constant Summary collapse

NAMESPACE =
'throttler:cache:'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(redis: Redis.new, default_ttl: 60) ⇒ RedisCache

Returns a new instance of RedisCache.



5
6
7
8
# File 'lib/faraday_throttler/redis_cache.rb', line 5

def initialize(redis: Redis.new, default_ttl: 60)
  @redis = redis
  @default_ttl = default_ttl
end

Instance Method Details

#get(key, default = nil) ⇒ Object



16
17
18
# File 'lib/faraday_throttler/redis_cache.rb', line 16

def get(key, default = nil)
  redis.get([NAMESPACE, key].join) || default
end

#set(key, value, ex: default_ttl) ⇒ Object



10
11
12
13
14
# File 'lib/faraday_throttler/redis_cache.rb', line 10

def set(key, value, ex: default_ttl)
  opts = {}
  opts[:ex] = ex if ex > 0
  redis.set [NAMESPACE, key].join, value, opts
end