diff --git a/lib/rack/cache/redis_entitystore.rb b/lib/rack/cache/redis_entitystore.rb index 75a0402..1907e9e 100644 --- a/lib/rack/cache/redis_entitystore.rb +++ b/lib/rack/cache/redis_entitystore.rb @@ -1,4 +1,5 @@ require 'rack/cache/entitystore' +require 'redis-rack-cache/constants' module Rack module Cache @@ -36,11 +37,8 @@ def write(body, ttl=0) buf = StringIO.new key, size = slurp(body){|part| buf.write(part) } - if ttl.zero? - [key, size] if cache.set(key, buf.string) - else - [key, size] if cache.setex(key, ttl, buf.string) - end + ttl = ::Redis::Rack::Cache::DEFAULT_TTL if ttl.zero? + [key, size] if cache.setex(key, ttl, buf.string) end def purge(key) diff --git a/lib/rack/cache/redis_metastore.rb b/lib/rack/cache/redis_metastore.rb index 2084cf5..157fb57 100644 --- a/lib/rack/cache/redis_metastore.rb +++ b/lib/rack/cache/redis_metastore.rb @@ -2,6 +2,7 @@ require 'rack/utils' require 'rack/cache/key' require 'rack/cache/metastore' +require 'redis-rack-cache/constants' module Rack module Cache @@ -30,11 +31,8 @@ def read(key) end def write(key, entries, ttl=0) - if ttl.zero? - cache.set(hexdigest(key), entries) - else - cache.setex(hexdigest(key), ttl, entries) - end + ttl = ::Redis::Rack::Cache::DEFAULT_TTL if ttl.zero? + cache.setex(hexdigest(key), ttl, entries) end def purge(key) diff --git a/lib/redis-rack-cache/constants.rb b/lib/redis-rack-cache/constants.rb new file mode 100644 index 0000000..6e91020 --- /dev/null +++ b/lib/redis-rack-cache/constants.rb @@ -0,0 +1,7 @@ +class Redis + module Rack + module Cache + DEFAULT_TTL = 86_400 * 365 # 1 year + end + end +end