Class: Resourceful::MemcacheCacheManager
- Inherits:
-
AbstractCacheManager
- Object
- AbstractCacheManager
- Resourceful::MemcacheCacheManager
- Defined in:
- lib/resourceful/memcache_cache_manager.rb
Instance Method Summary collapse
-
#initialize(*memcache_servers) ⇒ MemcacheCacheManager
constructor
Create a new Memcached backed cache manager.
-
#invalidate(uri) ⇒ Object
Invalidates a all cached entries for a uri.
-
#lookup(request) ⇒ Resourceful::Response?
Finds a previously cached response to the provided request.
-
#store(request, response) ⇒ Object
Store a response in the cache.
Constructor Details
#initialize(*memcache_servers) ⇒ MemcacheCacheManager
Create a new Memcached backed cache manager
12 13 14 |
# File 'lib/resourceful/memcache_cache_manager.rb', line 12 def initialize(*memcache_servers) @memcache = MemCache.new(memcache_servers, :multithread => true) end |
Instance Method Details
#invalidate(uri) ⇒ Object
Invalidates a all cached entries for a uri.
This is used, for example, to invalidate the cache for a resource that gets POSTed to.
60 61 62 |
# File 'lib/resourceful/memcache_cache_manager.rb', line 60 def invalidate(uri) @memcache.delete(uri_hash(uri)) end |
#lookup(request) ⇒ Resourceful::Response?
Finds a previously cached response to the provided request. The response returned may be stale.
25 26 27 28 29 30 31 32 |
# File 'lib/resourceful/memcache_cache_manager.rb', line 25 def lookup(request) resp = cache_entries_for(request)[request] return if resp.nil? resp. = false resp end |
#store(request, response) ⇒ Object
Store a response in the cache.
This method is smart enough to not store responses that cannot be cached (Vary: * or Cache-Control: no-cache, private, …)
44 45 46 47 48 49 50 51 |
# File 'lib/resourceful/memcache_cache_manager.rb', line 44 def store(request, response) return unless response.cachable? entries = cache_entries_for(request) entries[request] = response @memcache[request.to_mc_key] = entries end |