class Rack::Cache::MetaStore
The MetaStore is responsible for storing meta information about a request/response pair keyed by the request’s URL.
The meta store keeps a list of request/response pairs for each canonical request URL. A request/response pair is a two element Array of the form:
[request, response]
The request element is a Hash of Rack environment keys. Only protocol keys (i.e., those that start with “HTTP_”) are stored. The response element is a Hash of cached HTTP response headers for the paired request.
The MetaStore class is abstract and should not be instanstiated directly. Concrete subclasses should implement the protected read, write, and purge methods. Care has been taken to keep these low-level methods dumb and straight-forward to implement.
Constants
- DISK
-
Concrete
MetaStoreimplementation that stores request/response pairs on disk. - FILE
-
Concrete
MetaStoreimplementation that stores request/response pairs on disk. - GAE
- GAECACHE
- HEAP
-
Concrete
MetaStoreimplementation that uses a simple Hash to store request/response pairs on the heap. - MEM
-
Concrete
MetaStoreimplementation that uses a simple Hash to store request/response pairs on the heap. - MEMCACHE
- MEMCACHED
Public Instance Methods
Source
# File lib/rack/cache/meta_store.rb, line 112 def cache_key(request) keygen = request.env['rack-cache.cache_key'] || Key keygen.call(request) end
Generate a cache key for the request.
Source
# File lib/rack/cache/meta_store.rb, line 118 def invalidate(request, entity_store) modified = false key = cache_key(request) entries = read(key).map do |req, res| response = restore_response(res) if response.fresh? response.expire! modified = true end [req, persist_response(response)] end write key, entries if modified end
Invalidate all cache entries that match the request.
Source
# File lib/rack/cache/meta_store.rb, line 28 def lookup(request, entity_store) key = cache_key(request) entries = read(key) # bail out if we have nothing cached return nil if entries.empty? # find a cached entry that matches the request. env = request.env match = entries.detect{ |req,res| requests_match?((res['vary'] || res['vary']), env, req) } return nil if match.nil? _, res = match entity_key = res['x-content-digest'] if entity_key && body = entity_store.open(entity_key) restore_response(res, body) else # the metastore referenced an entity that doesn't exist in # the entitystore, purge the entry from the meta-store begin purge(key) rescue NotImplementedError @@warned_on_purge ||= begin warn "WARNING: Future releases may require purge implementation for #{self.class.name}" true end nil end end end
Locate a cached response for the request provided. Returns a Rack::Cache::Response object if the cache hits or nil if no cache entry was found.
Source
# File lib/rack/cache/meta_store.rb, line 62 def store(request, response, entity_store) key = cache_key(request) stored_env = persist_request(request) # write the response body to the entity store if this is the # original response. if response.headers['x-content-digest'].nil? if request.env['rack-cache.use_native_ttl'] && response.fresh? digest, size = entity_store.write(response.body, response.ttl) else digest, size = entity_store.write(response.body) end response.headers['x-content-digest'] = digest response.headers['content-length'] = size.to_s unless response.headers['Transfer-Encoding'] # If the entitystore backend is a Noop, do not try to read the body from the backend, it always returns an empty array unless entity_store.is_a? Rack::Cache::EntityStore::Noop # A stream body can only be read once and is currently closed by #write. # (To avoid having to keep giant objects in memory when writing to disk cache # the body is never converted to a single string) # We cannot always reply on body to be re-readable, # so we have to read it from the cache. # BUG: if the cache was unable to store a stream, the stream will be closed # and rack will try to read it again, resulting in hard to track down exception response.body = entity_store.open(digest) || response.body end end # read existing cache entries, remove non-varying, and add this one to # the list vary = response.vary entries = read(key).reject do |env, res| (vary == (res['vary'])) && requests_match?(vary, env, stored_env) end headers = persist_response(response) headers.delete('age') entries.unshift [stored_env, headers] if request.env['rack-cache.use_native_ttl'] && response.fresh? write key, entries, response.ttl else write key, entries end key end
Write a cache entry to the store under the given key. Existing entries are read and any that match the response are removed. This method calls write with the new list of cache entries.
Protected Instance Methods
Source
# File lib/rack/cache/meta_store.rb, line 185 def purge(key) raise NotImplementedError end
Remove all cached entries at the key specified. No error is raised when the key does not exist.
Source
# File lib/rack/cache/meta_store.rb, line 172 def read(key) raise NotImplementedError end
Locate all cached request/response pairs that match the specified URL key. The result must be an Array of all cached request/response pairs. An empty Array must be returned if nothing is cached for the specified key.
Source
# File lib/rack/cache/meta_store.rb, line 179 def write(key, negotiations, ttl = nil) raise NotImplementedError end
Store an Array of request/response pairs for the given key. Concrete implementations should not attempt to filter or concatenate the list in any way.