Class: TravelAdapter::Adapter
- Inherits:
-
Object
- Object
- TravelAdapter::Adapter
- Defined in:
- lib/traveladapter/adapter.rb
Overview
Versatile wrapper around key-value stores, providing abstraction and expiration facility.
Instance Method Summary collapse
-
#decrement(key) ⇒ Numeric
Decrement a counter stored under a particular key.
-
#delete(key) ⇒ Boolean
Delete a key-value pair stored with a particular key.
-
#get(key) ⇒ Object
(also: #[])
Get the value stored with a particular key.
-
#get_ttl(key) ⇒ Integer
Get expiration time.
-
#has_key?(key) ⇒ Boolean
Check if anything is stored with a particular key.
-
#increment(key) ⇒ Numeric
Increment a counter stored under a particular key.
-
#initialize(store = nil) ⇒ Adapter
constructor
Create a new instance of TravelAdapter::Adapter object.
-
#keys(pattern = /.*/) ⇒ Array
Get non-expired keys stored.
-
#set(key, value, ttl = (10 << 32)) ⇒ Object
(also: #[]=)
Set the value stored with a particular key.
-
#set_ttl(key, ttl) ⇒ Time
Change how long the value is to be stored.
Constructor Details
#initialize(store = nil) ⇒ Adapter
Create a new instance of TravelAdapter::Adapter object.
If no object is passed, the Adapter will internally use a Hash.
15 16 17 18 19 20 21 22 |
# File 'lib/traveladapter/adapter.rb', line 15 def initialize(store=nil) @semaphore = Mutex::new if store.nil? @store = Hash::new else @store = store end end |
Instance Method Details
#decrement(key) ⇒ Numeric
Decrement a counter stored under a particular key. A counter can be any numeric object, as long as it responds sensibly to + and - messages.
30 31 32 |
# File 'lib/traveladapter/adapter.rb', line 30 def decrement(key) change_by(key, -1) end |
#delete(key) ⇒ Boolean
Delete a key-value pair stored with a particular key.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/traveladapter/adapter.rb', line 38 def delete(key) begin @semaphore.synchronize do atomic_delete(key) atomic_delete(marshal_key(key)) atomic_delete(expiration_key(key)) end return true rescue Exception return false end end |
#get(key) ⇒ Object Also known as: []
Get the value stored with a particular key.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/traveladapter/adapter.rb', line 56 def get(key) return nil unless atomic_has_key?(key) if expired?(key) delete(key) return nil elsif needs_marshaling?(key) return Marshal::load(atomic_get(key)) else return atomic_get(key) end end |
#get_ttl(key) ⇒ Integer
Get expiration time.
133 134 135 136 |
# File 'lib/traveladapter/adapter.rb', line 133 def get_ttl(key) expires = atomic_get(expiration_key(key)) return expires.nil? ? nil : (expires.to_i - Time::now.to_i) end |
#has_key?(key) ⇒ Boolean
Check if anything is stored with a particular key.
a value, otherwise false.
73 74 75 |
# File 'lib/traveladapter/adapter.rb', line 73 def has_key?(key) !get(key).nil? end |
#increment(key) ⇒ Numeric
Increment a counter stored under a particular key. A counter can be any numeric object, as long as it responds sensibly to + and - messages.
83 84 85 |
# File 'lib/traveladapter/adapter.rb', line 83 def increment(key) change_by(key, 1) end |
#keys(pattern = /.*/) ⇒ Array
Get non-expired keys stored.
91 92 93 94 95 96 |
# File 'lib/traveladapter/adapter.rb', line 91 def keys(pattern=/.*/) re = /\A__(expire|marshal)__:/ return atomic_keys.select do |k| k.match(re).nil? and not expired?(k) and !k.match(pattern).nil? end end |
#set(key, value, ttl = (10 << 32)) ⇒ Object Also known as: []=
Set the value stored with a particular key.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/traveladapter/adapter.rb', line 104 def set(key, value, ttl=(10 << 32)) @semaphore.synchronize do if value.is_a?(String) atomic_set(marshal_key(key), "0") else atomic_set(marshal_key(key), "1") value = Marshal::dump(value) end atomic_set(key, value) set_ttl(key, ttl) end return value end |
#set_ttl(key, ttl) ⇒ Time
Change how long the value is to be stored.
123 124 125 126 127 |
# File 'lib/traveladapter/adapter.rb', line 123 def set_ttl(key, ttl) new_ttl = Time::now.to_i + ttl atomic_set(expiration_key(key), new_ttl.to_s) return Time::at(new_ttl) end |