Class: Moneta::Adapters::MemcachedDalli

Inherits:
Object
  • Object
show all
Includes:
Defaults, ExpiresSupport
Defined in:
lib/moneta/adapters/memcached/dalli.rb

Overview

Memcached backend (using gem dalli)

Instance Attribute Summary

Attributes included from ExpiresSupport

#default_expires

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #fetch, #key?

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ MemcachedDalli

Returns a new instance of MemcachedDalli

Parameters:

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

Options Hash (options):

  • :server (String) — default: '127.0.0.1:11211'

    Memcached server

  • :expires (Integer)

    Default expiration time

  • Other (Object)

    options passed to `Dalli::Client#new`


15
16
17
18
19
# File 'lib/moneta/adapters/memcached/dalli.rb', line 15

def initialize(options = {})
  self.default_expires = options.delete(:expires)
  server = options.delete(:server) || '127.0.0.1:11211'
  @cache = ::Dalli::Client.new(server, options)
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

63
64
65
66
# File 'lib/moneta/adapters/memcached/dalli.rb', line 63

def clear(options = {})
  @cache.flush_all
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil


69
70
71
72
# File 'lib/moneta/adapters/memcached/dalli.rb', line 69

def close
  @cache.close
  nil
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 `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


38
39
40
41
42
# File 'lib/moneta/adapters/memcached/dalli.rb', line 38

def delete(key, options = {})
  value = @cache.get(key)
  @cache.delete(key)
  value
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

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

Returns:

  • (Object)

    value from store


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/moneta/adapters/memcached/dalli.rb', line 45

def increment(key, amount = 1, options = {})
  # FIXME: There is a Dalli bug, load(key) returns a wrong value after increment
  # therefore we set default = nil and create the counter manually
	# See https://github.com/mperham/dalli/issues/309
  result = if amount >= 0
             @cache.incr(key, amount, expires_value(options) || nil, nil)
           else
             @cache.decr(key, -amount, expires_value(options) || nil, nil)
           end
  if result
    result
  else
    store(key, amount, options)
    amount
  end
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 `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


22
23
24
25
26
27
28
29
# File 'lib/moneta/adapters/memcached/dalli.rb', line 22

def load(key, options = {})
  value = @cache.get(key)
  if value
    expires = expires_value(options, nil)
    @cache.set(key, value, expires || nil, :raw => true) if expires != nil
    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 `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


32
33
34
35
# File 'lib/moneta/adapters/memcached/dalli.rb', line 32

def store(key, value, options = {})
  @cache.set(key, value, expires_value(options) || nil, :raw => true)
  value
end