Class: Moneta::Adapters::TokyoTyrant
- Inherits:
-
Object
- Object
- Moneta::Adapters::TokyoTyrant
- Includes:
- Defaults, HashAdapter
- Defined in:
- lib/moneta/adapters/tokyotyrant.rb
Overview
TokyoTyrant backend
Constant Summary collapse
- ENOREC =
error code: no record found
7
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Instance Method Summary collapse
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ TokyoTyrant
constructor
A new instance of TokyoTyrant.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#slice(*keys, **options) ⇒ <(Object, Object)>
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods included from HashAdapter
#clear, #fetch_values, #key?, #merge!
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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 27 def initialize( = {}) [:host] ||= '127.0.0.1' [:port] ||= 1978 if [:backend] @backend = [:backend] elsif defined?(::TokyoTyrant::RDB) # Use ruby client @backend = ::TokyoTyrant::RDB.new @backend.open([:host], [:port]) or error else # Use native client @backend = ::TokyoTyrant::DB.new([:host], [:port]) end @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 Attribute Details
#backend ⇒ Object (readonly)
21 22 23 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 21 def backend @backend end |
Instance Method Details
#close ⇒ Object
Explicitly close the store
90 91 92 93 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 90 def close @backend.close nil end |
#create(key, value, options = {}) ⇒ Boolean
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.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 76 def create(key, value, = {}) 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
62 63 64 65 66 67 68 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 62 def delete(key, = {}) value = load(key, ) if value @backend.delete(key) or error value end end |
#increment(key, amount = 1, options = {}) ⇒ Object
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.
71 72 73 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 71 def increment(key, amount = 1, = {}) @backend.addint(key, amount) or error end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
48 49 50 51 52 53 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 48 def load(key, = {}) 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)>
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#==).
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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 96 def slice(*keys, **) 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
56 57 58 59 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 56 def store(key, value, = {}) @backend.put(key, pack(value)) or error value end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
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.
112 113 114 115 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 112 def values_at(*keys, **) hash = slice(*keys, **) keys.map { |key| hash[key] } end |