Class: Moneta::Adapters::TokyoTyrant

Inherits:
Moneta::Adapter show all
Includes:
HashAdapter
Defined in:
lib/moneta/adapters/tokyotyrant.rb

Overview

TokyoTyrant backend

Constant Summary collapse

ENOREC =

error code: no record found

7

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods included from HashAdapter

#clear, #fetch_values, #key?, #merge!

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!, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ TokyoTyrant

Returns a new instance of TokyoTyrant.

Parameters:

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

Options Hash (options):

  • :host (String) — default: '127.0.0.1'

    Server host name

  • :port (Integer) — default: 1978

    Server port

  • :backend (::TokyoTyrant::RDB)

    Use existing backend instance



37
38
39
40
41
42
43
44
# File 'lib/moneta/adapters/tokyotyrant.rb', line 37

def initialize(options = {})
  super
  @native = backend.class.name != 'TokyoTyrant::RDB'
  probe = '__tokyotyrant_endianness_probe'
  backend.delete(probe)
  backend.addint(probe, 1)
  @pack = backend.delete(probe) == [1].pack('l>') ? 'l>' : 'l<'
end

Instance Method Details

#closeObject

Explicitly close the store

Returns:

  • nil



89
90
91
92
# File 'lib/moneta/adapters/tokyotyrant.rb', line 89

def close
  backend.close
  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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/moneta/adapters/tokyotyrant.rb', line 75

def create(key, value, options = {})
  if @native
    begin
      # Native client throws an exception
      backend.putkeep(key, pack(value))
    rescue TokyoTyrantError
      false
    end
  else
    backend.putkeep(key, pack(value))
  end
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



61
62
63
64
65
66
67
# File 'lib/moneta/adapters/tokyotyrant.rb', line 61

def delete(key, options = {})
  value = load(key, options)
  if value
    backend.delete(key) or error
    value
  end
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



70
71
72
# File 'lib/moneta/adapters/tokyotyrant.rb', line 70

def increment(key, amount = 1, options = {})
  backend.addint(key, amount) or error
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



47
48
49
50
51
52
# File 'lib/moneta/adapters/tokyotyrant.rb', line 47

def load(key, options = {})
  value = backend[key]
  # raise if there is an error and the error is not "no record"
  error if value == nil && backend.ecode != ENOREC
  value && unpack(value)
end

#slice(*keys, **options) ⇒ <(Object, Object)>

Note:

The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

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, Object)>)

    A collection of key-value pairs



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/moneta/adapters/tokyotyrant.rb', line 95

def slice(*keys, **options)
  hash =
    if @native
      backend.mget(*keys)
    else
      hash = Hash[keys.map { |key| [key] }]
      raise unless backend.mget(hash) >= 0
      hash
    end

  hash.each do |key, value|
    hash[key] = unpack(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



55
56
57
58
# File 'lib/moneta/adapters/tokyotyrant.rb', line 55

def store(key, value, options = {})
  backend.put(key, pack(value)) or error
  value
end

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

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:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values



111
112
113
114
# File 'lib/moneta/adapters/tokyotyrant.rb', line 111

def values_at(*keys, **options)
  hash = slice(*keys, **options)
  keys.map { |key| hash[key] }
end