Skip to content

Commit

Permalink
Merge pull request #8 from Sage/enforce_default_ttl
Browse files Browse the repository at this point in the history
Enforce default TTL always
  • Loading branch information
vaughanbrittonsage authored Feb 5, 2019
2 parents 01735d1 + d5861e3 commit e9922b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v2.1.0

* Set method enforces a default TTL of `3_600` if the `expires_in` option is not provided, or is invalid.
* Set method enforces the TTL to always be an Integer.

# v2.0.0

* Dropped Support for JRuby
Expand Down
10 changes: 8 additions & 2 deletions lib/cache_store_redis/redis_cache_store.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This class is used to implement a redis cache store.
class RedisCacheStore
# Default expiry time if not provided. (1 hour)
DEFAULT_TTL = 3_600

def initialize(namespace = nil, config = nil)
@connection_pool = RedisConnectionPool.new(config)

Expand Down Expand Up @@ -59,7 +62,7 @@ def with_client
# @param key [String] This is the unique key to reference the value being set within this cache store.
# @param value [Object] This is the value to set within this cache store.
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
def set(key, value, expires_in = 3_600)
def set(key, value, expires_in = DEFAULT_TTL)
k = build_key(key)

v = if value.nil? || (value.is_a?(String) && value.strip.empty?)
Expand All @@ -68,11 +71,14 @@ def set(key, value, expires_in = 3_600)
serialize(value)
end

expiry_int = Integer(expires_in)
expire_value = expiry_int.positive? ? expiry_int : DEFAULT_TTL

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

client.expire(k, expires_in) if expires_in.positive?
client.expire(k, expire_value)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cache_store_redis/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CacheStoreRedis
VERSION = '2.0.0'
VERSION = '2.1.0'
end
29 changes: 29 additions & 0 deletions spec/cache_store_redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@
end

describe "#set" do
context 'expires_in' do
let(:key) { SecureRandom.uuid }
let(:value) { 'SomeValue' }

it 'will always set a default TTL if one is not provided' do
expect_any_instance_of(Redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
expect_any_instance_of(Redis).to receive(:expire).with("test:#{key}", 3_600)
@cache_store.set(key, value)
end

it 'will always set a default TTL if an invalid one is provided' do
expect_any_instance_of(Redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
expect_any_instance_of(Redis).to receive(:expire).with("test:#{key}", 3_600)
@cache_store.set(key, value, -200)
end

it 'will always set a default TTL if an invalid one is provided' do
expect_any_instance_of(Redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
expect_any_instance_of(Redis).to receive(:expire).with("test:#{key}", 3_600)
@cache_store.set(key, value, 0.456)
end

it 'will always force the TTL to be an integer' do
expect_any_instance_of(Redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
expect_any_instance_of(Redis).to receive(:expire).with("test:#{key}", 20)
@cache_store.set(key, value, 20.123)
end
end

it 'should add a string to the cache store and retrieve it' do
key = SecureRandom.uuid
value = 'value123'
Expand Down

0 comments on commit e9922b3

Please sign in to comment.