Class: RedisCacheStore

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

Overview

This class is used to implement a redis cache store.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RedisCacheStore.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cache_store_redis/redis_cache_store.rb', line 3

def initialize(namespace = nil, config = nil)
  unless RUBY_PLATFORM == 'java'
    require 'oj'
  end

  @namespace = namespace
  @config = config
  @queue = Queue.new

  @connections_created = 0
  @connections_in_use = 0
  @mutex = Mutex.new
  @enable_stats = false
end

Instance Method Details

#cleanObject



75
76
77
78
79
80
# File 'lib/cache_store_redis/redis_cache_store.rb', line 75

def clean
  while @queue.length.positive?
    client = @queue.pop(true)
    client.close
  end
end

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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cache_store_redis/redis_cache_store.rb', line 41

def configure(host = 'localhost',
              port = 6379,
              db = 'default',
              password = nil,
              driver: nil,
              url: nil,
              connect_timeout: 0.5,
              read_timeout: 1,
              write_timeout: 0.5)
  if !url.nil?
    @config = {}
    @config[:url] = url
    @config[:db] = db
  else
    @config = { host: host, port: port, db: db }
  end

  @config[:password] = password unless password.nil?
  @config[:driver] = driver unless driver.nil?

  @config[:connect_timeout] = connect_timeout
  @config[:read_timeout] = read_timeout
  @config[:write_timeout] = write_timeout
end

#decrement_using_statObject



34
35
36
37
38
# File 'lib/cache_store_redis/redis_cache_store.rb', line 34

def decrement_using_stat
  @mutex.synchronize do
    @connections_in_use -= 1
  end
end

#enable_stats=(value) ⇒ Object



18
19
20
# File 'lib/cache_store_redis/redis_cache_store.rb', line 18

def enable_stats=(value)
  @enable_stats = value
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.



169
170
171
172
173
# File 'lib/cache_store_redis/redis_cache_store.rb', line 169

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

#fetch_clientObject



66
67
68
69
70
71
72
73
# File 'lib/cache_store_redis/redis_cache_store.rb', line 66

def fetch_client
  begin
    @queue.pop(true)
  rescue
    increment_created_stat
    Redis.new(@config)
  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.

(This is used in conjunction with the block to hydrate the cache key if it is empty.) when it is not found.

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.

  • &block (Block)

    This block is provided to hydrate this cache store with the value for the request key

Returns:

  • (Object)

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cache_store_redis/redis_cache_store.rb', line 135

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

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

  if !value.nil? && value.strip.empty?
    value = nil
  else
    value = deserialize(value) unless value.nil?
  end

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

  value
end

#increment_created_statObject



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

def increment_created_stat
  @mutex.synchronize do
    @connections_created += 1
  end
end

#increment_using_statObject



28
29
30
31
32
# File 'lib/cache_store_redis/redis_cache_store.rb', line 28

def increment_using_stat
  @mutex.synchronize do
    @connections_in_use += 1
  end
end

#log_statsObject



82
83
84
85
86
87
88
# File 'lib/cache_store_redis/redis_cache_store.rb', line 82

def log_stats
  return unless @enable_stats == true
  S1Logging.logger.debug do
    "[#{self.class}] - REDIS Connection Stats. Process: #{Process.pid} | " \
"Created: #{@connections_created} | Pending: #{@queue.length} | In use: #{@connections_in_use}"
  end
end

#pingString

Ping the cache store.

Returns:

  • (String)

    PONG



178
179
180
181
182
# File 'lib/cache_store_redis/redis_cache_store.rb', line 178

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.



159
160
161
162
163
# File 'lib/cache_store_redis/redis_cache_store.rb', line 159

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.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cache_store_redis/redis_cache_store.rb', line 109

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

  v = if value.nil? || (value.is_a?(String) && value.strip.empty?)
    nil
  else
    serialize(value)
  end

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

      client.expire(k, expires_in) if expires_in.positive?
    end
  end
end

#with_clientObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cache_store_redis/redis_cache_store.rb', line 90

def with_client
  log_stats
  begin
    client = fetch_client
    increment_using_stat
    log_stats
    yield client
  ensure
    @queue.push(client)
    decrement_using_stat
    log_stats
  end
end