Class: FMCache::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fmcache/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, notifier = nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • client (Redis | MockRedis)
  • notifier (Proc) (defaults to: nil)


5
6
7
8
# File 'lib/fmcache/client.rb', line 5

def initialize(client, notifier = nil)
  @client   = client
  @notifier = notifier
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/fmcache/client.rb', line 10

def client
  @client
end

#notifierObject (readonly)

Returns the value of attribute notifier.



10
11
12
# File 'lib/fmcache/client.rb', line 10

def notifier
  @notifier
end

Instance Method Details

#del(keys:) ⇒ Boolean

Parameters:

  • keys (<String>)

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
# 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:) ⇒ { String => { String => String } }

Parameters:

  • keys (<String>)
  • fields (<String>)

Returns:

  • ({ String => { String => String } })


31
32
33
34
35
36
37
38
39
40
41
42
43
# 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:) ⇒ Boolean

Parameters:

  • keys (<String>)
  • fields (<String>)

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
# 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:) ⇒ Boolean

Parameters:

  • values ({ String => { String => String } })

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
# 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