kindkid / lrucache

A simple LRU-cache based on a hash and a priority queue.
MIT License
28 stars 7 forks source link

Unexpected behaviour with a max_size of 0 #4

Closed jamiecobbett closed 11 years ago

jamiecobbett commented 11 years ago

If you create an LRUCache with a max_size of 0, it actually effectively creates a cache with a max_size of 1.

cache = LRUCache.new(max_size: 0)
=> #<LRUCache:0x00000004a01c30 @max_size=0, @default=nil, @ttl=0.0, @soft_ttl=0.0, @retry_delay=0.0, @pqueue=<PriorityQueue: []>, @data={}, @counter=0>
cache.store("a", 1)
=> 1
cache.keys
=> ["a"]
cache.store("b", 2)
=> 2
cache.keys
=> ["b"]

Whilst I appreciate that it might be an odd thing to do, it's something we did to (try to) disable the cache. I think there are two options: change the code to actually enforce a size of 0 or to raise an error if passed a size of 0.

I'm happy to try writing a Pull Request for either one, though I think the latter will be simpler.

jamiecobbett commented 11 years ago

Closing in favour of https://github.com/kindkid/lrucache/pull/5