Class: Waves::Caches::Memcached
- Defined in:
- lib/waves/caches/memcached.rb
Overview
A simple interface to Memcached. Pass in the memcached server to the constructor, like this:
Waves::Caches::Memcached.new( 'localhost:11211' )
Memcached::NotFound is converted to nil, even though nil is, in fact, a valid value to want to cache. Rather than force every use of the cache interface to add exception handling for the (relatively rare) case where nil is an expected value, just code those cases using the #exists? method.
This interface is not thread-safe. If you want to use this in apps where you are handling requests in parallel, use SynchronizedMemcached.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(args) ⇒ Memcached
constructor
A new instance of Memcached.
- #store(key, value, ttl = 0, marshal = true) ⇒ Object
Methods inherited from Simple
Constructor Details
#initialize(args) ⇒ Memcached
Returns a new instance of Memcached.
21 22 23 24 |
# File 'lib/waves/caches/memcached.rb', line 21 def initialize( args ) raise ArgumentError, ":servers is nil" if args[ :servers ].nil? @cache = ::Memcached.new( args[ :servers ], args[ :options ] || {} ) end |
Instance Method Details
#clear ⇒ Object
40 41 42 |
# File 'lib/waves/caches/memcached.rb', line 40 def clear @cache.flush end |
#delete(key) ⇒ Object
36 37 38 |
# File 'lib/waves/caches/memcached.rb', line 36 def delete( key ) @cache.delete( key.to_s ) end |
#fetch(key) ⇒ Object
30 31 32 33 34 |
# File 'lib/waves/caches/memcached.rb', line 30 def fetch( key ) @cache.get( key.to_s ) rescue ::Memcached::NotFound => e nil end |
#store(key, value, ttl = 0, marshal = true) ⇒ Object
26 27 28 |
# File 'lib/waves/caches/memcached.rb', line 26 def store( key, value, ttl = 0, marshal = true ) @cache.add( key.to_s, value, ttl, marshal ) end |