class WebFetch::Storage::Redis

Public Class Methods

new(client = nil) click to toggle source
# File lib/web_fetch/storage/redis.rb, line 6
def initialize(client = nil)
  require 'redis' if client.nil?
  @client = client || ::Redis
  @config = {
    host: ENV.fetch('WEB_FETCH_REDIS_HOST', 'localhost'),
    port: ENV.fetch('WEB_FETCH_REDIS_PORT', '6379')
  }
end

Public Instance Methods

delete(key) click to toggle source
# File lib/web_fetch/storage/redis.rb, line 26
def delete(key)
  storage.del(key)
end
fetch(key) click to toggle source
# File lib/web_fetch/storage/redis.rb, line 19
def fetch(key)
  result = storage.get(key)
  return JSON.parse(result, symbolize_names: true) unless result.nil?

  nil
end
store(key, obj) click to toggle source
# File lib/web_fetch/storage/redis.rb, line 15
def store(key, obj)
  storage.set(key, obj.to_json, ex: ttl)
end

Private Instance Methods

storage() click to toggle source
# File lib/web_fetch/storage/redis.rb, line 32
def storage
  @storage ||= begin
    host = @config.fetch(:host)
    port = @config.fetch(:port)
    @client.new(url: "redis://#{host}:#{port}")
  end
end
ttl() click to toggle source
# File lib/web_fetch/storage/redis.rb, line 40
def ttl
  @ttl ||= ENV.fetch('WEB_FETCH_REDIS_TTL', '60').to_i
end