Class: Moneta::Adapters::TokyoTyrant
- Inherits:
-
Object
- Object
- Moneta::Adapters::TokyoTyrant
- Includes:
- Defaults, HashAdapter
- Defined in:
- lib/moneta/adapters/tokyotyrant.rb
Overview
TokyoTyrant backend
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.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from HashAdapter
Methods included from Defaults
#[], #[]=, #decrement, #features, #fetch, included, #key?, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ TokyoTyrant
Returns a new instance of TokyoTyrant.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 24 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 raise @backend.errmsg(@backend.ecode) 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)
18 19 20 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 18 def backend @backend end |
Instance Method Details
#close ⇒ Object
Explicitly close the store
85 86 87 88 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 85 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.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 71 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
57 58 59 60 61 62 63 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 57 def delete(key, = {}) value = load(key, ) if value @backend.delete(key) 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.
66 67 68 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 66 def increment(key, amount = 1, = {}) @backend.addint(key, amount) || raise('Tried to increment non integer value') end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
45 46 47 48 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 45 def load(key, = {}) value = @backend[key] value && unpack(value) end |
#store(key, value, options = {}) ⇒ Object
Store value with key
51 52 53 54 |
# File 'lib/moneta/adapters/tokyotyrant.rb', line 51 def store(key, value, = {}) @backend[key] = pack(value) value end |