Permalink
Browse files

Raise an exception when a TTL greater than 30 days is provided since …

…memcache does not support offsets greater than that.
  • Loading branch information...
1 parent 535a23d commit d0f1eb5afebdbd66ff8d4ceedd0b5fa29a2651bc @lukemelia committed May 7, 2008
Showing with 15 additions and 1 deletion.
  1. +1 −0 lib/acts_as_cached.rb
  2. +7 −1 lib/acts_as_cached/cache_methods.rb
  3. +7 −0 test/cache_test.rb
@@ -48,4 +48,5 @@ def acts_as_cached(options = {})
class CacheException < StandardError; end
class NoCacheStore < CacheException; end
class NoGetMulti < CacheException; end
+ class InvalidTtl < CacheException; end
end
@@ -77,7 +77,8 @@ def get_caches(*args)
def set_cache(cache_id, value, ttl = nil)
returning(value) do |v|
v = @@nil_sentinel if v.nil?
- cache_store(:set, cache_key(cache_id), v, ttl || cache_config[:ttl] || 1500)
+ entry_ttl = check_valid_ttl(ttl || cache_config[:ttl] || 1500)
+ cache_store(:set, cache_key(cache_id), v, entry_ttl)
end
end
@@ -218,6 +219,11 @@ def autoload_missing_constants
if error.to_s[/undefined class|referred/] && !lazy_load[error.to_s.split.last.constantize] then retry
else raise error end
end
+
+ def check_valid_ttl(ttl)
+ raise InvalidTtl if ttl > 30.days
+ ttl
+ end
end
module InstanceMethods
View
@@ -268,6 +268,13 @@ class << @cache; undef :get_multi end
Story.expects(:set_cache).with(1, @story, ttl)
@story.set_cache(ttl)
end
+
+ # TTL values greater than the number of seconds in 30 days are considered real Unix time rather than an offset.
+ # We raise an error in that situation as it is not the expected behavior.
+ # See in "Expiration times" in http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
+ specify "should not be able to set a ttl greater than 30 days" do
+ proc { Story.set_cache(1, @story, 30.days + 1) }.should.raise(ActsAsCached::InvalidTtl)
+ end
specify "should be able to cache arbitrary instance methods using caches" do
Story.cache_store.expects(:get).returns(nil)

0 comments on commit d0f1eb5

Please sign in to comment.