Class: Moneta::Adapters::MemcachedNative
- Inherits:
-
Object
- Object
- Moneta::Adapters::MemcachedNative
- Includes:
- Defaults, ExpiresSupport
- Defined in:
- lib/moneta/adapters/memcached/native.rb
Overview
Memcached backend (using gem memcached)
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Attributes included from ExpiresSupport
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this 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 = {}) ⇒ 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, #features, #fetch, included, #key?, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ MemcachedNative
Returns a new instance of MemcachedNative.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/moneta/adapters/memcached/native.rb', line 20 def initialize( = {}) server = .delete(:server) || '127.0.0.1:11211' self.default_expires = .delete(:expires) @backend = [:backend] || begin .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) ::Memcached.new(server, ) end end |
Instance Attribute Details
#backend ⇒ Object (readonly)
12 13 14 |
# File 'lib/moneta/adapters/memcached/native.rb', line 12 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
84 85 86 87 |
# File 'lib/moneta/adapters/memcached/native.rb', line 84 def clear( = {}) @backend.flush self 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.
76 77 78 79 80 81 |
# File 'lib/moneta/adapters/memcached/native.rb', line 76 def create(key, value, = {}) @backend.add(key, value, expires_value() || 0, false) true rescue ::Memcached::ConnectionDataExists false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
52 53 54 55 56 57 |
# File 'lib/moneta/adapters/memcached/native.rb', line 52 def delete(key, = {}) value = @backend.get(key, false) @backend.delete(key) value rescue ::Memcached::NotFound 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/moneta/adapters/memcached/native.rb', line 60 def increment(key, amount = 1, = {}) result = if amount >= 0 @backend.increment(key, amount) else @backend.decrement(key, -amount) end # HACK: Throw error if applied to invalid value # see https://github.com/evan/memcached/issues/110 Utils.to_int((@backend.get(key, false) rescue nil)) if result == 0 result rescue ::Memcached::NotFound => ex retry unless create(key, amount.to_s, ) amount end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
34 35 36 37 38 39 40 41 42 |
# File 'lib/moneta/adapters/memcached/native.rb', line 34 def load(key, = {}) value = @backend.get(key, false) if value expires = expires_value(, nil) @backend.set(key, value, expires || 0, false) if expires != nil value end rescue ::Memcached::NotFound end |
#store(key, value, options = {}) ⇒ Object
Store value with key
45 46 47 48 49 |
# File 'lib/moneta/adapters/memcached/native.rb', line 45 def store(key, value, = {}) # TTL must be Fixnum @backend.set(key, value, expires_value() || 0, false) value end |