Class: RedisCacheStore

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

Overview

This class is used to implement a redis cache store. This class is used for interacting with a redis based cache store.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace = nil, config = nil) ⇒ RedisCacheStore

Returns a new instance of RedisCacheStore.



28
29
30
31
32
33
34
35
36
# File 'lib/cache_store_redis.rb', line 28

def initialize(namespace = nil, config = nil)

  if RUBY_PLATFORM != 'java'
    require 'oj'
  end

  @namespace = namespace
  @config = config
end

Class Method Details

.pool(config) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/cache_store_redis.rb', line 10

def self.pool(config)
  @pool ||= ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
    if config == nil
      Redis.new
    else
      Redis.new(config)
    end
  end
end

.pool_sizeObject



20
21
22
# File 'lib/cache_store_redis.rb', line 20

def self.pool_size
  @pool_size ||= Integer(ENV['CACHE_STORE_POOL_SIZE'] || 20)
end

.pool_timeoutObject



24
25
26
# File 'lib/cache_store_redis.rb', line 24

def self.pool_timeout
  @pool_size ||= Integer(ENV['CACHE_STORE_POOL_TIMEOUT'] || 1)
end

Instance Method Details

#configure(host = 'localhost', port = 6379, db = 'default', password = nil, driver: nil, url: nil) ⇒ Object

This method is called to configure the connection to the cache store.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cache_store_redis.rb', line 52

def configure(host = 'localhost', port = 6379, db = 'default', password = nil, driver: nil, url: nil)
  if url != nil
    @config = {}
    @config[:url] = url
    @config[:db] = db
  else
    @config = { :host => host, :port => port, :db => db }
  end
  if password != nil
    @config[:password] = password
  end
  if driver != nil
    @config[:driver] = driver
  end
end

#exist?(key) ⇒ Boolean

This method is called to check if a value exists within this cache store for a specific key.

Parameters:

  • key (String)

    This is the unique key to reference the value to check for within this cache store.

Returns:

  • (Boolean)

    True or False to specify if the key exists in the cache store.



130
131
132
133
134
# File 'lib/cache_store_redis.rb', line 130

def exist?(key)
  with_client do |client|
    client.exists(build_key(key))
  end
end

#get(key, expires_in = 0, &block) ⇒ Object

This method is called to get a value from this cache store by it’s unique key.

Parameters:

  • key (String)

    This is the unique key to reference the value to fetch from within this cache store.

  • expires_in (Integer) (defaults to: 0)

    This is the number of seconds from the current time that this value should expire. (This is used in conjunction with the block to hydrate the cache key if it is empty.)

  • &block (Block)

    This block is provided to hydrate this cache store with the value for the request key when it is not found.

Returns:

  • (Object)

    The value for the specified unique key within the cache store.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cache_store_redis.rb', line 99

def get(key, expires_in = 0, &block)

  k = build_key(key)

  value = with_client do |client|
    client.get(k)
  end

  value = deserialize(value) unless value == nil

  if value.nil? && block_given?
    value = yield
    set(k, value, expires_in)
  end

  return value
end

#pingString

Ping the cache store.

Returns:

  • (String)

    PONG



139
140
141
142
143
# File 'lib/cache_store_redis.rb', line 139

def ping
  with_client do |client|
    client.ping
  end
end

#remove(key) ⇒ Object

This method is called to remove a value from this cache store by it’s unique key.

Parameters:

  • key (String)

    This is the unique key to reference the value to remove from this cache store.



120
121
122
123
124
# File 'lib/cache_store_redis.rb', line 120

def remove(key)
  with_client do |client|
    client.del(build_key(key))
  end
end

#set(key, value, expires_in = 0) ⇒ Object

This method is called to set a value within this cache store by it’s key.

Parameters:

  • key (String)

    This is the unique key to reference the value being set within this cache store.

  • value (Object)

    This is the value to set within this cache store.

  • expires_in (Integer) (defaults to: 0)

    This is the number of seconds from the current time that this value should expire.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cache_store_redis.rb', line 73

def set(key, value, expires_in = 0)
  v = nil
  k = build_key(key)

  if value != nil
    v = serialize(value)
  end

  with_client do |client|
    client.multi do
      client.set(k, v)

      if expires_in > 0
        client.expire(k, expires_in)
      end
    end
  end

end

#with_clientObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cache_store_redis.rb', line 38

def with_client
  if self.class.pool_size > 0
    self.class.pool(@config).with(timeout: self.class.pool_timeout) do |client|
      raise 'nil redis client returned from pool' if client.nil?
      yield client
    end
  else
    # allow single client to be used when pool size is zero
    @client ||= Redis.new(@config)
    yield @client
  end
end