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:

- a maximum number of elements
- the maximum amount of memory used for all elements
- the allowed memory-size per element
- time to live

Differences to the original implementation include:

- The Cache is now a Struct for speed
- Object memory size is obtained via Marshal::dump instead of to_s

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.

### Methods

[] []= clear delete each_key each_pair each_value empty? expire fetch index invalidate invalidate_all key? keys length new size statistics store to_hash value? values

### Included Modules

Enumerable

### Constants

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

### Public Class methods

new(options = {}, &hook)

### Public Instance methods

[](key)
[]=(key, obj)
clear()
delete(key)
each_key() {|key| ...}
each_pair() {|key, obj.content| ...}
each_value() {|obj.content| ...}
empty?()
expire()
fetch(key, default = (p_default = true; {|key| ...}

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'

index(given_value)
invalidate(key)

Alias for delete

invalidate_all()

Alias for clear

key?(key)
keys()
length()

Alias for size

size()
statistics()
store(key, value)
to_hash()
value?(given_value)
values()

[Validate]