Class: Moneta::Adapters::MemcachedNative

Inherits:
Moneta::Adapter show all
Includes:
ExpiresSupport
Defined in:
lib/moneta/adapters/memcached/native.rb

Overview

Memcached backend (using gem memcached)

Instance Attribute Summary

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods included from ExpiresSupport

included

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

Methods included from Defaults

#[], #[]=, #decrement, #each_key, #features, #fetch, #fetch_values, included, #key?, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Object

Parameters:

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

Options Hash (options):

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

    Memcached server

  • :namespace (String)

    Key namespace

  • :expires (Integer) — default: 604800

    Default expiration time

  • :backend (::Memcached)

    Use existing backend instance

  • Other (Object)

    options passed to ‘Memcached#new`



19
20
21
22
23
24
25
# File 'lib/moneta/adapters/memcached/native.rb', line 19

backend do |server: '127.0.0.1:11211', namespace: nil, **options|
  options[:prefix_key] = namespace if namespace
  # We don't want a limitation on the key charset. Therefore we use the binary protocol.
  # It is also faster.
  options[:binary_protocol] = true unless options.include?(:binary_protocol)
  ::Memcached.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: {})


87
88
89
90
# File 'lib/moneta/adapters/memcached/native.rb', line 87

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

#closeObject

Explicitly close the store

Returns:

  • nil



93
94
95
96
# File 'lib/moneta/adapters/memcached/native.rb', line 93

def close
  @backend.reset
  nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

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

Atomically sets a key to value if it’s not set.

Parameters:

  • key (Object)
  • value (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)

Returns:

  • (Boolean)

    key was set



77
78
79
80
81
82
83
84
# File 'lib/moneta/adapters/memcached/native.rb', line 77

def create(key, value, options = {})
  expires = expires_value(options)
  Numeric === expires and expires = expires.to_i
  @backend.add(key, value, expires || 0, false)
  true
rescue ::Memcached::ConnectionDataExists
  false
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



52
53
54
55
56
57
58
# File 'lib/moneta/adapters/memcached/native.rb', line 52

def delete(key, options = {})
  value = @backend.get(key, false)
  @backend.delete(key)
  value
rescue ::Memcached::NotFound
  nil
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: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/moneta/adapters/memcached/native.rb', line 61

def increment(key, amount = 1, options = {})
  result = if amount >= 0
             @backend.increment(key, amount)
           else
             @backend.decrement(key, -amount)
           end
  # HACK: Throw error if applied to invalid value
  # see https://github.com/evan/memcached/issues/110
  Integer((@backend.get(key, false) rescue 0)) if result == 0
  result
rescue ::Memcached::NotFound
  retry unless create(key, amount.to_s, options)
  amount
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



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moneta/adapters/memcached/native.rb', line 28

def load(key, options = {})
  value = @backend.get(key, false)
  if value
    expires = expires_value(options, nil)
    unless expires == nil
      Numeric === expires and expires = expires.to_i
      @backend.set(key, value, expires || 0, false)
    end
    value
  end
rescue ::Memcached::NotFound
  nil
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



43
44
45
46
47
48
49
# File 'lib/moneta/adapters/memcached/native.rb', line 43

def store(key, value, options = {})
  # TTL must be Integer
  expires = expires_value(options)
  Numeric === expires and expires = expires.to_i
  @backend.set(key, value, expires || 0, false)
  value
end