Parent

Files

Class/Module Index [+]

Quicksearch

Moneta::Adapters::LRUHash

LRUHash backend

Based on Hashery::LRUHash but simpler and measures both memory usage and hash size.

@api public

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [Integer] :max_size (1024000) Maximum total byte size of hash values @option options [Integer] :max_count (10240) Maximum number of hash values

# File lib/moneta/adapters/lruhash.rb, line 16
def initialize(options = {})
  @max_size = options[:max_size] || 1024000
  @max_count = options[:max_count] || 10240
  clear
end

Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

# File lib/moneta/adapters/lruhash.rb, line 60
def clear(options = {})
  @entry, @size = {}, 0
  @list = Entry.new
  @list.prev = @list.next = @list
  self
end
delete(key, options = {}) click to toggle source

(see Proxy#delete)

# File lib/moneta/adapters/lruhash.rb, line 51
def delete(key, options = {})
  if entry = @entry.delete(key)
    @size -= entry.value.bytesize
    entry.unlink
    entry.value
  end
end
key?(key, options = {}) click to toggle source

(see Proxy#key?)

# File lib/moneta/adapters/lruhash.rb, line 23
def key?(key, options = {})
  @entry.key?(key)
end
load(key, options = {}) click to toggle source

(see Proxy#load)

# File lib/moneta/adapters/lruhash.rb, line 28
def load(key, options = {})
  if entry = @entry[key]
    entry.insert_after(@list)
    entry.value
  end
end
store(key, value, options = {}) click to toggle source

(see Proxy#store)

# File lib/moneta/adapters/lruhash.rb, line 36
def store(key, value, options = {})
  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

[Validate]

Generated with the Darkfish Rdoc Generator 2.