class Moneta::Adapters::LRUHash

LRUHash backend

Based on {rubygems.org/gems/lru_redux lru_redux} but 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 byte size of all values, nil disables the limit @option options [Integer] :max_value (options) Maximum byte size of one value, nil disables the limit @option options [Integer] :max_count (10240) Maximum number of values, nil disables the limit

Calls superclass method Moneta::Adapter::new
# File lib/moneta/adapters/lruhash.rb, line 25
def initialize(options = {})
  super
  clear
end

Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

# File lib/moneta/adapters/lruhash.rb, line 79
def clear(options = {})
  backend.clear
  @size = 0
  self
end
delete(key, options = {}) click to toggle source

(see Proxy#delete)

# File lib/moneta/adapters/lruhash.rb, line 71
def delete(key, options = {})
  if value = backend.delete(key) and config.max_size
    @size -= value.bytesize
  end
  value
end
drop(options = {}) click to toggle source

Drops the least-recently-used pair, if any

@param [Hash] options Options to merge @return [(Object, String), nil] The dropped pair, if any

# File lib/moneta/adapters/lruhash.rb, line 89
def drop(options = {})
  if key = backend.keys.first
    [key, delete(key)]
  end
end
each_key() { |k| ... } click to toggle source

(see Proxy#each_key)

# 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 = {}) click to toggle source

(see Proxy#key?)

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

(see Proxy#load)

# File lib/moneta/adapters/lruhash.rb, line 46
def load(key, options = {})
  if value = backend.delete(key)
    backend[key] = value
    value
  end
end
store(key, value, options = {}) click to toggle source

(see Proxy#store)

# File lib/moneta/adapters/lruhash.rb, line 54
def store(key, value, options = {})
  if config.max_value && value.bytesize > config.max_value
    delete(key)
  else
    if config.max_size
      if old_value = backend.delete(key)
        @size -= old_value.bytesize
      end
      @size += value.bytesize
    end
    backend[key] = value
    drop while config.max_size && @size > config.max_size || config.max_count && backend.size > config.max_count
  end
  value
end