From 8fa5fe558c7f7af5a596b3f30a753bbf810ac791 Mon Sep 17 00:00:00 2001 From: Jamie Cobbett Date: Fri, 5 Jul 2013 13:09:17 +0100 Subject: [PATCH] Gracefully handle a max_size of 0 If you tried to create an LRUCache with a max_size of 0, it actually effectively creates a cache with a max_size of 1. ```ruby cache = LRUCache.new(max_size: 0) => #, @data={}, @counter=0> cache.store("a", 1) => 1 cache.keys => ["a"] cache.store("b", 2) => 2 cache.keys => ["b"] ``` --- lib/lrucache.rb | 2 +- spec/lrucache_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/lrucache.rb b/lib/lrucache.rb index 735873c..221a63a 100644 --- a/lib/lrucache.rb +++ b/lib/lrucache.rb @@ -13,7 +13,7 @@ def initialize(opts={}) @ttl = Float(opts[:ttl] || 0) @soft_ttl = Float(opts[:soft_ttl] || 0) @retry_delay = Float(opts[:retry_delay] || 0) - raise "max_size must not be negative" if @max_size < 0 + raise "max_size must be greater than 0" if @max_size < 1 raise "ttl must not be negative" if @ttl < 0 raise "soft_ttl must not be negative" if @soft_ttl < 0 raise "retry_delay must not be negative" if @retry_delay < 0 diff --git a/spec/lrucache_spec.rb b/spec/lrucache_spec.rb index 8a1e00e..0e8a2d9 100644 --- a/spec/lrucache_spec.rb +++ b/spec/lrucache_spec.rb @@ -10,8 +10,9 @@ it "should raise an exception if :max_size parameter can't be converted to an integer" do expect { LRUCache.new(:max_size => "moocow") }.to raise_exception end - it "should raise an exception if :max_size parameter is converted to a negative integer" do + it "should raise an exception if :max_size parameter is converted to a non-positive integer" do expect { LRUCache.new(:max_size => -1) }.to raise_exception + expect { LRUCache.new(:max_size => 0) }.to raise_exception end it "should default :default to nil" do