Class: EOAT::Cache::MemcachedCache
- Inherits:
-
Object
- Object
- EOAT::Cache::MemcachedCache
- Defined in:
- lib/eoat/cache/memcached_cache.rb
Overview
Memcached cache handler. Used gem memcache
Default use standard connection parameters.
Instance Method Summary collapse
-
#get(host, uri) ⇒ Object, NilClass
Get object from cache.
-
#initialize(server = 'localhost:11211', prefix = 'eoat') ⇒ MemcachedCache
constructor
A new instance of MemcachedCache.
-
#save(host, uri, content) ⇒ Object
Save instance of result class.
Constructor Details
#initialize(server = 'localhost:11211', prefix = 'eoat') ⇒ MemcachedCache
Returns a new instance of MemcachedCache.
15 16 17 18 19 |
# File 'lib/eoat/cache/memcached_cache.rb', line 15 def initialize(server='localhost:11211', prefix='eoat') require 'memcache' @backend = Memcache.new(:server => server, :namespace => prefix, :segment_large_values => true) end |
Instance Method Details
#get(host, uri) ⇒ Object, NilClass
Get object from cache
26 27 28 29 30 |
# File 'lib/eoat/cache/memcached_cache.rb', line 26 def get(host, uri) # Set key as md5 string key = EOAT::Cache.md5hash(host + uri) @backend.get(key) end |
#save(host, uri, content) ⇒ Object
Save instance of result class.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/eoat/cache/memcached_cache.rb', line 36 def save(host, uri, content) # Calculate TTL in seconds expire = (content.cached_until - content.request_time).to_i # If TTL > EOAT.max_ttl set EOAT.max_tt as expire expire = expire > EOAT.max_ttl ? EOAT.max_ttl : expire # If 0 or a negative value, it does not save. if expire > 0 # Set key as md5 string key = EOAT::Cache.md5hash(host + uri) @backend.set( key, content, :expiry => expire ) end end |