Class: Rack::Cache::MetaStore

### Home Classes Methods

### In Files

- lib/rack/cache/metastore.rb

### Parent

Object

### Namespace

- CLASS Rack::Cache::MetaStore::DISK
- CLASS Rack::Cache::MetaStore::Dalli
- CLASS Rack::Cache::MetaStore::Disk
- CLASS Rack::Cache::MetaStore::FILE
- CLASS Rack::Cache::MetaStore::GAE
- CLASS Rack::Cache::MetaStore::GAECACHE
- CLASS Rack::Cache::MetaStore::GAEStore
- CLASS Rack::Cache::MetaStore::HEAP
- CLASS Rack::Cache::MetaStore::Heap
- CLASS Rack::Cache::MetaStore::MEM
- CLASS Rack::Cache::MetaStore::MemCacheBase
- CLASS Rack::Cache::MetaStore::MemCached

### Methods

- #cache_key
- #invalidate
- #lookup
- #purge
- #read
- #store
- #write

### Files

- CHANGES
- COPYING
- README
- TODO

### Class/Module Index

Quicksearch

- Rack
- Rack::Cache
- Rack::Cache::AppEngine
- Rack::Cache::AppEngine::MC
- Rack::Cache::AppEngine::MemCache
- Rack::Cache::CacheControl
- Rack::Cache::Context
- Rack::Cache::EntityStore
- Rack::Cache::EntityStore::DISK
- Rack::Cache::EntityStore::Dalli
- Rack::Cache::EntityStore::Disk
- Rack::Cache::EntityStore::FILE
- Rack::Cache::EntityStore::GAE
- Rack::Cache::EntityStore::GAECACHE
- Rack::Cache::EntityStore::GAEStore
- Rack::Cache::EntityStore::HEAP
- Rack::Cache::EntityStore::Heap
- Rack::Cache::EntityStore::MEM
- Rack::Cache::EntityStore::MemCacheBase
- Rack::Cache::EntityStore::MemCached
- Rack::Cache::Key
- Rack::Cache::MetaStore
- Rack::Cache::MetaStore::DISK
- Rack::Cache::MetaStore::Dalli
- Rack::Cache::MetaStore::Disk
- Rack::Cache::MetaStore::FILE
- Rack::Cache::MetaStore::GAE
- Rack::Cache::MetaStore::GAECACHE
- Rack::Cache::MetaStore::GAEStore
- Rack::Cache::MetaStore::HEAP
- Rack::Cache::MetaStore::Heap
- Rack::Cache::MetaStore::MEM
- Rack::Cache::MetaStore::MemCacheBase
- Rack::Cache::MetaStore::MemCached
- Rack::Cache::Options
- Rack::Cache::Request
- Rack::Cache::Response
- Rack::Cache::Storage

No matching classes.

# 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
FILE
GAE
GAECACHE
HEAP
MEM
MEMCACHE
MEMCACHED

### Public Instance Methods

cache_key(request) click to toggle source

Generate a cache key for the request.

    # File lib/rack/cache/metastore.rb, line 88
    def cache_key(request)
      keygen = request.env['rack-cache.cache_key'] || Key
      keygen.call(request)
    end

invalidate(request, entity_store) click to toggle source

Invalidate all cache entries that match the request.

    # File lib/rack/cache/metastore.rb, line 94
    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
            [req, persist_response(response)]
          else
            [req, res]
          end
        end
      write key, entries if modified
    end

lookup(request, entity_store) click to toggle source

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.

    # File lib/rack/cache/metastore.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'], env, req)}
      return nil if match.nil?

      _, res = match
      if body = entity_store.open(res['X-Content-Digest'])
        restore_response(res, body)
      else
        # TODO the metastore referenced an entity that doesn't exist in
        # the entitystore. we definitely want to return nil but we should
        # also purge the entry from the meta-store when this is detected.
      end
    end

store(request, response, entity_store) click to toggle source

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.

    # File lib/rack/cache/metastore.rb, line 53
    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']
        response.body = entity_store.open(digest)
      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]
      write key, entries
      key
    end

### Protected Instance Methods

purge(key) click to toggle source

Remove all cached entries at the key specified. No error is raised when the key does not exist.

    # File lib/rack/cache/metastore.rb, line 163
    def purge(key)
      raise NotImplemented
    end

read(key) click to toggle source

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.

    # File lib/rack/cache/metastore.rb, line 150
    def read(key)
      raise NotImplemented
    end

write(key, negotiations) click to toggle source

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.

    # File lib/rack/cache/metastore.rb, line 157
    def write(key, negotiations)
      raise NotImplemented
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.