Class: Moneta::Adapters::MemcachedNative
- Inherits:
-
Object
- Object
- Moneta::Adapters::MemcachedNative
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/memcached/native.rb
Overview
Memcached backend (using gem memcached)
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#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 = {}) ⇒ MemcachedNative
constructor
A new instance of MemcachedNative.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #close, #decrement, #fetch, #key?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ MemcachedNative
Returns a new instance of MemcachedNative.
15 16 17 18 19 20 21 22 23 |
# File 'lib/moneta/adapters/memcached/native.rb', line 15 def initialize( = {}) server = .delete(:server) || '127.0.0.1:11211' @expires = .delete(:expires) || 604800 .merge!(:prefix_key => .delete(:namespace)) if [:namespace] # We don't want a limitation on the key charset. Therefore we use the binary protocol. # It is also faster. [:binary_protocol] = true unless .include?(:binary_protocol) @cache = ::Memcached.new(server, ) end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
70 71 72 73 |
# File 'lib/moneta/adapters/memcached/native.rb', line 70 def clear( = {}) @cache.flush self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
44 45 46 47 48 49 |
# File 'lib/moneta/adapters/memcached/native.rb', line 44 def delete(key, = {}) value = @cache.get(key, false) @cache.delete(key) value rescue ::Memcached::NotFound end |
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key
Not every Moneta store implements this method, a NotImplementedError if it is not supported.
This method also accepts negative amounts.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/moneta/adapters/memcached/native.rb', line 52 def increment(key, amount = 1, = {}) result = if amount >= 0 @cache.increment(key, amount) else @cache.decrement(key, -amount) end # HACK: Throw error if applied to invalid value if result == 0 value = @cache.get(key, false) rescue nil raise 'Tried to increment non integer value' unless value.to_s == value.to_i.to_s end result rescue ::Memcached::NotFound => ex store(key, amount.to_s, ) amount end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
26 27 28 29 30 31 32 33 34 |
# File 'lib/moneta/adapters/memcached/native.rb', line 26 def load(key, = {}) value = @cache.get(key, false) if value && .include?(:expires) store(key, value, ) else value end rescue ::Memcached::NotFound end |
#store(key, value, options = {}) ⇒ Object
Store value with key
37 38 39 40 41 |
# File 'lib/moneta/adapters/memcached/native.rb', line 37 def store(key, value, = {}) # TTL must be Fixnum @cache.set(key, value, [:expires] || @expires, false) value end |