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
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.
-
#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, included, #increment, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ LRUHash
Returns a new instance of LRUHash.
16 17 18 19 20 |
# File 'lib/moneta/adapters/lruhash.rb', line 16 def initialize( = {}) @max_size = [:max_size] || 1024000 @max_count = [:max_count] || 10240 clear end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
60 61 62 63 64 65 |
# File 'lib/moneta/adapters/lruhash.rb', line 60 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
51 52 53 54 55 56 57 |
# File 'lib/moneta/adapters/lruhash.rb', line 51 def delete(key, = {}) if entry = @entry.delete(key) @size -= entry.value.bytesize entry.unlink entry.value end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
23 24 25 |
# File 'lib/moneta/adapters/lruhash.rb', line 23 def key?(key, = {}) @entry.key?(key) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
28 29 30 31 32 33 |
# File 'lib/moneta/adapters/lruhash.rb', line 28 def load(key, = {}) if entry = @entry[key] entry.insert_after(@list) entry.value end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/moneta/adapters/lruhash.rb', line 36 def store(key, value, = {}) if entry = @entry[key] @size -= entry.value.bytesize else @entry[key] = entry = Entry.new entry.key = key end entry.value = value @size += entry.value.bytesize entry.insert_after(@list) delete(@list.prev.key) while @list.next != @list.prev && (@size > @max_size || @entry.size > @max_count) value end |