Class: Moneta::Adapters::MemcachedDalli

Inherits:
Base
  • Object
show all
Defined in:
lib/moneta/adapters/memcached/dalli.rb

Overview

Memcached backend (using gem dalli)

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Mixins::WithOptions

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ MemcachedDalli

Constructor

Parameters:

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

Options Hash (options):

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

    Memcached server

  • :expires (String)

    Default expiration time

  • Other (Object)

    options passed to ‘Dalli::Client#new`



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

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

Instance Method Details

#clear(options = {}) ⇒ Object



56
57
58
59
# File 'lib/moneta/adapters/memcached/dalli.rb', line 56

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

#closeObject



61
62
63
64
# File 'lib/moneta/adapters/memcached/dalli.rb', line 61

def close
  @cache.close
  nil
end

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



34
35
36
37
38
# File 'lib/moneta/adapters/memcached/dalli.rb', line 34

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

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



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

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
  result = if amount >= 0
             @cache.incr(key, amount, options[:expires], nil)
           else
             @cache.decr(key, -amount, options[:expires], nil)
           end
  if result
    result
  else
    store(key, amount, options)
    amount
  end
end

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



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

def load(key, options = {})
  value = @cache.get(key)
  if value && options.include?(:expires)
    store(key, value, options)
  else
    value
  end
end

#store(key, value, options = {}) ⇒ Object



29
30
31
32
# File 'lib/moneta/adapters/memcached/dalli.rb', line 29

def store(key, value, options = {})
  @cache.set(key, value, options[:expires], :raw => true)
  value
end