Class | Ramaze::LRUHash |
In: |
lib/ramaze/snippets/ramaze/lru_hash.rb
|
Parent: | Struct.new(:max_total, :max_value, :max_count, :expiration, :hook, :objs, :total_size, :list, :hits, :misses) |
A Hash-alike LRU cache that provides fine-grained control over content restrictions.
It allows you to set:
Differences to the original implementation include:
Note that due to calculating object size with Marshal, you might have to do some evaluation as to how large your values will be when marshaled, for example a String will have String#size + 10. This differs from object to object and between versions of Marshal, so be generous.
Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org> Copyright (c) 2009 Michael Fellinger <manveru@rubyists.com>
You may redistribute it and/or modify it under the same terms as Ruby.
CacheObject | = | Struct.new(:content, :size, :atime) | ||
VERSION | = | '0.3' | ||
KeyError | = | Module.const_defined?(:KeyError) ? KeyError : IndexError | On 1.8 we raise IndexError, on 1.9 we raise KeyError |
Note that this method diverges from the default behaviour of the Ruby Hash. If the cache doesn‘t find content for the given key, it will store the given default instead. Optionally it also takes a block, the return value of the block is then stored and returned.
@example
lru = LRUHash.new lru.fetch(:a) # => KeyError: key not found: :a lru.fetch(:a, :b) # => :b lru.fetch(:a) # => :b lru.fetch(:c){|key| key.to_s } # => 'c' lru.fetch(:c) # => 'c'