Class: RedisCacheStore
- Inherits:
-
Object
- Object
- RedisCacheStore
- Defined in:
- lib/cache_store_redis/redis_cache_store.rb
Overview
This class is used to implement a redis cache store.
Constant Summary collapse
- DEFAULT_TTL =
Default expiry time if not provided. (1 hour)
3_600
Instance Attribute Summary collapse
-
#connection_pool ⇒ Object
readonly
Returns the value of attribute connection_pool.
Instance Method Summary collapse
- #clean ⇒ Object (also: #shutdown)
-
#configure(host = 'localhost', port = 6379, db = 0, 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.
-
#exist?(key) ⇒ Boolean
This method is called to check if a value exists within this cache store for a specific key.
-
#get(key, expires_in = 0) ⇒ Object
(also: #read)
This method is called to get a value from this cache store by it’s unique key.
-
#initialize(namespace = nil, config = nil) ⇒ RedisCacheStore
constructor
A new instance of RedisCacheStore.
-
#ping ⇒ String
Ping the cache store.
-
#remove(key) ⇒ Object
(also: #delete)
This method is called to remove a value from this cache store by it’s unique key.
-
#set(key, value, expires_in = DEFAULT_TTL) ⇒ Object
(also: #write)
This method is called to set a value within this cache store by it’s key.
- #with_client(&block) ⇒ Object
Constructor Details
#initialize(namespace = nil, config = nil) ⇒ RedisCacheStore
Returns a new instance of RedisCacheStore.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 6 def initialize(namespace = nil, config = nil) @connection_pool = RedisConnectionPool.new(namespace, config) @namespace = namespace @config = config @connections_created = 0 @connections_in_use = 0 @mutex = Mutex.new @enable_stats = false end |
Instance Attribute Details
#connection_pool ⇒ Object (readonly)
Returns the value of attribute connection_pool.
18 19 20 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 18 def connection_pool @connection_pool end |
Instance Method Details
#clean ⇒ Object Also known as: shutdown
47 48 49 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 47 def clean connection_pool.shutdown end |
#configure(host = 'localhost', port = 6379, db = 0, 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.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 21 def configure(host = 'localhost', port = 6379, db = 0, 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 connection_pool.config = @config end |
#exist?(key) ⇒ Boolean
This method is called to check if a value exists within this cache store for a specific key.
124 125 126 127 128 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 124 def exist?(key) with_client do |client| !client.exists(build_key(key)).zero? end end |
#get(key, expires_in = 0) ⇒ Object Also known as: read
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 90 def get(key, expires_in = 0) 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 |
#ping ⇒ String
Ping the cache store.
133 134 135 136 137 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 133 def ping with_client do |client| client.ping end end |
#remove(key) ⇒ Object Also known as: delete
This method is called to remove a value from this cache store by it’s unique key.
114 115 116 117 118 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 114 def remove(key) with_client do |client| client.del(build_key(key)) end end |
#set(key, value, expires_in = DEFAULT_TTL) ⇒ Object Also known as: write
This method is called to set a value within this cache store by it’s key.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 61 def set(key, value, expires_in = DEFAULT_TTL) k = build_key(key) v = if value.nil? || (value.is_a?(String) && value.strip.empty?) nil else serialize(value) end expiry_int = Integer(expires_in) expire_value = expiry_int.positive? ? expiry_int : Integer(DEFAULT_TTL) with_client do |client| client.multi do client.set(k, v) client.expire(k, expire_value) end end end |
#with_client(&block) ⇒ Object
52 53 54 |
# File 'lib/cache_store_redis/redis_cache_store.rb', line 52 def with_client(&block) connection_pool.with_connection(&block) end |