Class: Moneta::Adapters::MemcachedNative

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

Overview

Memcached backend (using gem memcached)

Instance Attribute Summary

Attributes included from ExpiresSupport

#default_expires

Instance Method Summary collapse

Methods included from Defaults

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

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ MemcachedNative

Returns a new instance of MemcachedNative.

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

  • Other (Object)

    options passed to ‘Memcached#new`



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

def initialize(options = {})
  server = options.delete(:server) || '127.0.0.1:11211'
  self.default_expires = options.delete(:expires)
  options.merge!(:prefix_key => options.delete(:namespace)) if options[: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)
  @cache = ::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: {})


70
71
72
73
# File 'lib/moneta/adapters/memcached/native.rb', line 70

def clear(options = {})
  @cache.flush
  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 ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



46
47
48
49
50
51
# File 'lib/moneta/adapters/memcached/native.rb', line 46

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/moneta/adapters/memcached/native.rb', line 54

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



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

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



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

def store(key, value, options = {})
  # TTL must be Fixnum
  @cache.set(key, value, expires_value(options) || 0, false)
  value
end