Class: Moneta::Adapters::Client
- Inherits:
-
Object
- Object
- Moneta::Adapters::Client
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/client.rb
Overview
Moneta client backend
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#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.
-
#each_key ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
- #features ⇒ Object
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #decrement, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 |
# File 'lib/moneta/adapters/client.rb', line 14 def initialize( = {}) @socket = if [:socket] UNIXSocket.open([:socket]) else TCPSocket.open([:host] || '127.0.0.1', [:port] || 9000) end end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
61 62 63 64 65 |
# File 'lib/moneta/adapters/client.rb', line 61 def clear( = {}) write(:clear, ) read_msg self end |
#close ⇒ Object
Explicitly close the store
68 69 70 71 |
# File 'lib/moneta/adapters/client.rb', line 68 def close @socket.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.
55 56 57 58 |
# File 'lib/moneta/adapters/client.rb', line 55 def create(key, value, = {}) write(:create, key, value, ) read_msg end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
43 44 45 46 |
# File 'lib/moneta/adapters/client.rb', line 43 def delete(key, = {}) write(:delete, key, ) read_msg end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/moneta/adapters/client.rb', line 74 def each_key raise NotImplementedError, 'each_key is not supported' unless supports?(:each_key) return enum_for(:each_key) unless block_given? begin write(:each_key) yield_break = false loop do write('NEXT') # A StopIteration error will be raised by this call if the server # reached the end of the enumeration. This will stop the loop # automatically. result = read_msg # yield_break will be true in the ensure block (below) if anything # happened during the yield to stop further enumeration. yield_break = true yield result yield_break = false end ensure write('BREAK') if yield_break read_msg # nil return from each_key end self end |
#features ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/moneta/adapters/client.rb', line 105 def features @features ||= begin write(:features) read_msg.freeze 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.
49 50 51 52 |
# File 'lib/moneta/adapters/client.rb', line 49 def increment(key, amount = 1, = {}) write(:increment, key, amount, ) read_msg end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
24 25 26 27 |
# File 'lib/moneta/adapters/client.rb', line 24 def key?(key, = {}) write(:key?, key, ) read_msg end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
30 31 32 33 |
# File 'lib/moneta/adapters/client.rb', line 30 def load(key, = {}) write(:load, key, ) read_msg end |
#store(key, value, options = {}) ⇒ Object
Store value with key
36 37 38 39 40 |
# File 'lib/moneta/adapters/client.rb', line 36 def store(key, value, = {}) write(:store, key, value, ) read_msg value end |