class FMCache::Client

Attributes

client[R]
notifier[R]

Public Class Methods

new(client, notifier = nil) click to toggle source

@param [Redis | MockRedis] client @param [Proc] notifier

# File lib/fmcache/client.rb, line 5
def initialize(client, notifier = nil)
  @client   = client
  @notifier = notifier
end

Public Instance Methods

del(keys:) click to toggle source

@param [<String>] keys @return [Boolean]

# File lib/fmcache/client.rb, line 47
def del(keys:)
  if keys.size > 0
    client.del(*keys)
  end
  true
rescue Redis::BaseConnectionError => e
  notify(e)
  false
end
get(keys:, fields:) click to toggle source

@param [<String>] keys @param [<String>] fields @return [{ String => { String => String } }]

# File lib/fmcache/client.rb, line 31
def get(keys:, fields:)
  return {} if keys.size == 0

  values = client.pipelined do
    keys.each do |key|
      client.mapped_hmget(key, *fields)
    end
  end
  keys.zip(values).to_h
rescue Redis::BaseConnectionError => e
  notify(e)
  keys.map { |k| [k, fields.map { |f| [f, nil] }.to_h] }.to_h
end
hdel(keys:, fields:) click to toggle source

@param [<String>] keys @param [<String>] fields @return [Boolean]

# File lib/fmcache/client.rb, line 60
def hdel(keys:, fields:)
  client.pipelined do
    keys.each do |key|
      fields.each do |field|
        client.hdel(key, field)
      end
    end
  end
  true
rescue Redis::BaseConnectionError => e
  notify(e)
  false
end
set(values:, ttl:) click to toggle source

@param [{ String => { String => String } }] values @return [Boolean]

# File lib/fmcache/client.rb, line 14
def set(values:, ttl:)
  client.pipelined do
    values.each do |h_key, h_values|
      client.mapped_hmset(h_key, h_values)
      client.expire(h_key, ttl)
    end
  end

  true
rescue Redis::BaseConnectionError => e
  notify(e)
  false
end

Private Instance Methods

notify(e) click to toggle source

@param [Exception] e

# File lib/fmcache/client.rb, line 77
def notify(e)
  notifier.call(e) if notifier
end