diff --git a/CHANGELOG.md b/CHANGELOG.md index 1420ccc..3bd96ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/cache_store_redis/redis_cache_store.rb b/lib/cache_store_redis/redis_cache_store.rb index ff4ecdf..dea9a5c 100644 --- a/lib/cache_store_redis/redis_cache_store.rb +++ b/lib/cache_store_redis/redis_cache_store.rb @@ -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) @@ -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?) @@ -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 diff --git a/lib/cache_store_redis/version.rb b/lib/cache_store_redis/version.rb index 63f36ff..a293ed2 100644 --- a/lib/cache_store_redis/version.rb +++ b/lib/cache_store_redis/version.rb @@ -1,3 +1,3 @@ module CacheStoreRedis - VERSION = '2.0.0' + VERSION = '2.1.0' end diff --git a/spec/cache_store_redis_spec.rb b/spec/cache_store_redis_spec.rb index cf60f1a..27fdafa 100644 --- a/spec/cache_store_redis_spec.rb +++ b/spec/cache_store_redis_spec.rb @@ -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'