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 Hashery::LRUHash but simpler and measures both memory usage and hash size.
Defined Under Namespace
Classes: Entry
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.
-
#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.
-
#slice(*keys, **options) ⇒ <(Object, Object)>
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods included from CreateSupport
Methods included from IncrementSupport
Methods included from Defaults
#[], #[]=, #close, #create, #decrement, #features, #fetch, #fetch_values, included, #increment, #merge!, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ LRUHash
Returns a new instance of LRUHash.
22 23 24 25 26 27 |
# File 'lib/moneta/adapters/lruhash.rb', line 22 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 84 |
# File 'lib/moneta/adapters/lruhash.rb', line 79 def clear( = {}) @entry, @size = {}, 0 @list = Entry.new @list.prev = @list.next = @list self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
70 71 72 73 74 75 76 |
# File 'lib/moneta/adapters/lruhash.rb', line 70 def delete(key, = {}) if entry = @entry.delete(key) @size -= entry.value.bytesize if @max_size entry.unlink entry.value 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.
35 36 37 38 39 40 |
# File 'lib/moneta/adapters/lruhash.rb', line 35 def each_key(&block) return enum_for(:each_key) { @entry.length } unless block_given? @entry.each_key { |k| yield(k) } self end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
30 31 32 |
# File 'lib/moneta/adapters/lruhash.rb', line 30 def key?(key, = {}) @entry.key?(key) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
43 44 45 46 47 48 |
# File 'lib/moneta/adapters/lruhash.rb', line 43 def load(key, = {}) if entry = @entry[key] entry.insert_after(@list) entry.value end end |
#slice(*keys, **options) ⇒ <(Object, Object)>
The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).
Some adapters may implement this method atomically. The default implmentation uses #values_at.
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.
97 98 99 100 101 102 103 104 |
# File 'lib/moneta/adapters/lruhash.rb', line 97 def slice(*keys, **) return super unless @entry.respond_to?(:slice) hash = @entry.slice(*keys) hash.each do |key, entry| entry.insert_after(@list) hash[key] = entry.value end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/moneta/adapters/lruhash.rb', line 51 def store(key, value, = {}) if @max_value && value.bytesize > @max_value delete(key) else if entry = @entry[key] @size -= entry.value.bytesize if @max_size else @entry[key] = entry = Entry.new entry.key = key end entry.value = value @size += entry.value.bytesize if @max_size entry.insert_after(@list) delete(@list.prev.key) while @list.next != @list.prev && (@max_size && @size > @max_size || @max_count && @entry.size > @max_count) end value end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.
Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.
87 88 89 90 91 92 93 94 |
# File 'lib/moneta/adapters/lruhash.rb', line 87 def values_at(*keys, **) @entry.values_at(*keys).map do |entry| if entry entry.insert_after(@list) entry.value end end end |