Class: Moneta::Adapters::LRUHash

Inherits:
Object
  • Object
show all
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

Methods included from CreateSupport

#create, included

Methods included from IncrementSupport

included, #increment

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.

Parameters:

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

Options Hash (options):

  • :max_size (Integer) — default: 1024000

    Maximum byte size of all values

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

    Maximum byte size of one value

  • :max_count (Integer) — default: 10240

    Maximum number of values


17
18
19
20
21
22
# File 'lib/moneta/adapters/lruhash.rb', line 17

def initialize(options = {})
  @max_size = options[:max_size] || 1024000
  @max_count = options[:max_count] || 10240
  @max_value = options[:max_value] || @max_size
  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: {})

66
67
68
69
70
71
# File 'lib/moneta/adapters/lruhash.rb', line 66

def clear(options = {})
  @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

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


57
58
59
60
61
62
63
# File 'lib/moneta/adapters/lruhash.rb', line 57

def delete(key, options = {})
  if entry = @entry.delete(key)
    @size -= entry.value.bytesize
    entry.unlink
    entry.value
  end
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)

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

def key?(key, options = {})
  @entry.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


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

def load(key, options = {})
  if entry = @entry[key]
    entry.insert_after(@list)
    entry.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


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/moneta/adapters/lruhash.rb', line 38

def store(key, value, options = {})
  if value.bytesize > @max_value
    delete(key)
  else
    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)
  end
  value
end