Class: Moneta::Adapters::LRUHash
- Inherits:
-
Object
- Object
- Moneta::Adapters::LRUHash
- Includes:
- CreateSupport, Defaults, IncrementSupport
- Defined in:
- lib/moneta/adapters/lruhash.rb
Overview
LRUHash backend
Based on lru_redux but measures both memory usage and hash size.
Constant Summary collapse
- DEFAULT_MAX_SIZE =
1024000
- DEFAULT_MAX_COUNT =
10240
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#drop(options = {}) ⇒ (Object, String)?
Drops the least-recently-used pair, if any.
-
#each_key(&block) ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#initialize(options = {}) ⇒ LRUHash
constructor
A new instance of LRUHash.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from CreateSupport
Methods included from IncrementSupport
Methods included from Defaults
#[], #[]=, #close, #create, #decrement, #features, #fetch, #fetch_values, included, #increment, #merge!, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ LRUHash
Returns a new instance of LRUHash.
23 24 25 26 27 28 |
# File 'lib/moneta/adapters/lruhash.rb', line 23 def initialize( = {}) @max_size = .fetch(:max_size) { DEFAULT_MAX_SIZE } @max_count = .fetch(:max_count) { DEFAULT_MAX_COUNT } @max_value = [[:max_value], @max_size].compact.min clear end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
79 80 81 82 83 |
# File 'lib/moneta/adapters/lruhash.rb', line 79 def clear( = {}) @backend = {} @size = 0 self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
71 72 73 74 75 76 |
# File 'lib/moneta/adapters/lruhash.rb', line 71 def delete(key, = {}) if value = @backend.delete(key) and @max_size @size -= value.bytesize end value end |
#drop(options = {}) ⇒ (Object, String)?
Drops the least-recently-used pair, if any
89 90 91 92 93 |
# File 'lib/moneta/adapters/lruhash.rb', line 89 def drop( = {}) if key = @backend.keys.first [key, delete(key)] end end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.
36 37 38 39 40 41 42 43 |
# File 'lib/moneta/adapters/lruhash.rb', line 36 def each_key(&block) return enum_for(:each_key) { @backend.length } unless block_given? # The backend needs to be duplicated because reading mutates this # store. @backend.dup.each_key { |k| yield(k) } self end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
31 32 33 |
# File 'lib/moneta/adapters/lruhash.rb', line 31 def key?(key, = {}) @backend.key?(key) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
46 47 48 49 50 51 |
# File 'lib/moneta/adapters/lruhash.rb', line 46 def load(key, = {}) if value = @backend.delete(key) @backend[key] = value value end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/moneta/adapters/lruhash.rb', line 54 def store(key, value, = {}) if @max_value && value.bytesize > @max_value delete(key) else if @max_size if old_value = @backend.delete(key) @size -= old_value.bytesize end @size += value.bytesize end @backend[key] = value drop while @max_size && @size > @max_size || @max_count && @backend.size > @max_count end value end |