Class: Moneta::Adapters::LRUHash

Inherits:
Moneta::Adapter show all
Includes:
CreateSupport, IncrementSupport
Defined in:
lib/moneta/adapters/lruhash.rb

Overview

LRUHash backend

Based on lru_redux but measures both memory usage and hash size.

Instance Attribute Summary

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods included from CreateSupport

#create, included

Methods included from IncrementSupport

included, #increment

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :max_size (Integer) — default: 1024000

    Maximum byte size of all values, nil disables the limit

  • :max_value (Integer) — default: options[:max_size]

    Maximum byte size of one value, nil disables the limit

  • :max_count (Integer) — default: 10240

    Maximum number of values, nil disables the limit



25
26
27
28
# File 'lib/moneta/adapters/lruhash.rb', line 25

def initialize(options = {})
  super
  clear
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


79
80
81
82
83
# File 'lib/moneta/adapters/lruhash.rb', line 79

def clear(options = {})
  backend.clear
  @size = 0
  self
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



71
72
73
74
75
76
# 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 = {}) ⇒ (Object, String)?

Drops the least-recently-used pair, if any

Parameters:

  • options (Hash) (defaults to: {})

    Options to merge

Returns:

  • ((Object, String), nil)

    The dropped pair, if any



89
90
91
92
93
# File 'lib/moneta/adapters/lruhash.rb', line 89

def drop(options = {})
  if key = backend.keys.first
    [key, delete(key)]
  end
end

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

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.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)


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

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


31
32
33
# File 'lib/moneta/adapters/lruhash.rb', line 31

def key?(key, options = {})
  backend.key?(key)
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



46
47
48
49
50
51
# 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 = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



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, 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