Class: Botbckt::Store
- Inherits:
-
Object
- Object
- Botbckt::Store
- Defined in:
- lib/botbckt/store.rb
Overview
Implements a basic key/value store API for cross-session state storage.
Currently, this class is Redis-backed, but any key/value store could be supported, in theory.
Instance Attribute Summary collapse
-
#backend ⇒ Object
Returns the value of attribute backend.
Instance Method Summary collapse
-
#get(key, &block) ⇒ Object
Retrieves the value stored at key.
-
#increment!(key, &block) ⇒ Object
Increments the value stored at key by 1, creating the key and initializing it to 0 if necessary.
-
#initialize(host, port) ⇒ Store
constructor
Parameters host<String>:: IP address or hostname of the Redis server.
-
#set(key, value, &block) ⇒ Object
Sets the key to the given value, creating the key if necessary.
Constructor Details
#initialize(host, port) ⇒ Store
Parameters
- host<String>
-
IP address or hostname of the Redis server. Required.
- port<Integer>
-
Port the Redis server is listening on. Required.
18 19 20 |
# File 'lib/botbckt/store.rb', line 18 def initialize(host, port) self.backend = EventMachine::Protocols::Redis.connect(host, port) end |
Instance Attribute Details
#backend ⇒ Object
Returns the value of attribute backend.
12 13 14 |
# File 'lib/botbckt/store.rb', line 12 def backend @backend end |
Instance Method Details
#get(key, &block) ⇒ Object
Retrieves the value stored at key. Returns nil if the key does not exist.
Parameters
- key<String>
-
The identifier to retrieve. Required.
- &block
-
A callback to execute after the value is retrieved. The block should take a single parameter: the value retrieved. Required.
41 42 43 44 |
# File 'lib/botbckt/store.rb', line 41 def get(key, &block) return unless block_given? backend.get(key, &block) end |
#increment!(key, &block) ⇒ Object
Increments the value stored at key by 1, creating the key and initializing it to 0 if necessary.
Parameters
- key<String>
-
The identifier whose value should be incremented. Required.
- &block
-
A callback to execute after the value is stored. The block should take a single parameter: the value stored. Optional.
54 55 56 |
# File 'lib/botbckt/store.rb', line 54 def increment!(key, &block) backend.incr(key, &block) end |
#set(key, value, &block) ⇒ Object
Sets the key to the given value, creating the key if necessary.
Parameters
- key<String>
-
The identifier for this value. Required.
- value<Object>
-
The value to store at the key. Required.
- &block
-
A callback to execute after the value is stored. The block should take a single parameter: the value stored. Optional.
30 31 32 |
# File 'lib/botbckt/store.rb', line 30 def set(key, value, &block) backend.set(key, value, &block) end |