Class: Renderful::Cache::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/renderful/cache/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Redis

Returns a new instance of Redis.



8
9
10
# File 'lib/renderful/cache/redis.rb', line 8

def initialize(redis)
  @redis = redis
end

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



6
7
8
# File 'lib/renderful/cache/redis.rb', line 6

def redis
  @redis
end

Instance Method Details

#delete(*keys) ⇒ Object



24
25
26
# File 'lib/renderful/cache/redis.rb', line 24

def delete(*keys)
  redis.del(*keys)
end

#delete_matched(pattern) ⇒ Object



28
29
30
31
# File 'lib/renderful/cache/redis.rb', line 28

def delete_matched(pattern)
  keys = redis.scan_each(match: pattern).to_a
  delete(*keys)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/renderful/cache/redis.rb', line 12

def exist?(key)
  redis.exists(key)
end

#fetch(key) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/renderful/cache/redis.rb', line 33

def fetch(key)
  return read(key) if exist?(key)

  yield.tap do |value|
    write(key, value)
  end
end

#read(key) ⇒ Object



16
17
18
# File 'lib/renderful/cache/redis.rb', line 16

def read(key)
  redis.get(key)
end

#write(key, value) ⇒ Object



20
21
22
# File 'lib/renderful/cache/redis.rb', line 20

def write(key, value)
  redis.set(key, value)
end