Coder Social home page Coder Social logo

rack-cache's Introduction

Rack::Cache

Rack::Cache is suitable as a quick drop-in component to enable HTTP caching for Rack-based applications that produce freshness (expires, cache-control) and/or validation (last-modified, etag) information:

  • Standards-based (RFC 2616)
  • Freshness/expiration based caching
  • Validation (if-modified-since / if-none-match)
  • vary support
  • cache-control public, private, max-age, s-maxage, must-revalidate, and proxy-revalidate.
  • Portable: 100% Ruby / works with any Rack-enabled framework
  • Disk, memcached, and heap memory storage backends

For more information about Rack::Cache features and usage, see:

https://rack.github.io/rack-cache/

Rack::Cache is not overly optimized for performance. The main goal of the project is to provide a portable, easy-to-configure, and standards-based caching solution for small to medium sized deployments. More sophisticated / high-performance caching systems (e.g., Varnish, Squid, httpd/mod-cache) may be more appropriate for large deployments with significant throughput requirements.

Installation

gem install rack-cache

Basic Usage

Rack::Cache is implemented as a piece of Rack middleware and can be used with any Rack-based application. If your application includes a rackup (.ru) file or uses Rack::Builder to construct the application pipeline, simply require and use as follows:

require 'rack/cache'

use Rack::Cache,
  metastore:    'file:/var/cache/rack/meta',
  entitystore:  'file:/var/cache/rack/body',
  verbose:      true

run app

Assuming you've designed your backend application to take advantage of HTTP's caching features, no further code or configuration is required for basic caching.

Using with Rails

# config/application.rb
config.action_dispatch.rack_cache = true
# or
config.action_dispatch.rack_cache = {
   verbose:     true,
   metastore:   'file:/var/cache/rack/meta',
   entitystore: 'file:/var/cache/rack/body'
}

You should now see Rack::Cache listed in the middleware pipeline:

rake middleware

more information

Using with Dalli

Dalli is a high performance memcached client for Ruby. More information at: https://github.com/mperham/dalli

require 'dalli'
require 'rack/cache'

use Rack::Cache,
  verbose:  true,
  metastore:    "memcached://localhost:11211/meta",
  entitystore:  "memcached://localhost:11211/body"

run app

Noop entity store

Does not persist response bodies (no disk/memory used).
Responses from the cache will have an empty body.
Clients must ignore these empty cached response (check for x-rack-cache response header).
Atm cannot handle streamed responses, patch needed.

require 'rack/cache'

use Rack::Cache,
 verbose: true,
 metastore: <any backend>
 entitystore: "noop:/"

run app

Ignoring tracking parameters in cache keys

It's fairly common to include tracking parameters which don't affect the content of the page. Since Rack::Cache uses the full URL as part of the cache key, this can cause unneeded churn in your cache. If you're using the default key class Rack::Cache::Key, you can configure a proc to ignore certain keys/values like so:

Rack::Cache::Key.query_string_ignore = proc { |k, v| k =~ /^(trk|utm)_/ }

License: MIT
Development

rack-cache's People

Contributors

rtomayko avatar grosser avatar amatriain avatar rmm5t avatar atzkey avatar mezis avatar nakajima avatar ykitamura-mdsol avatar aughr avatar jamesruston avatar heroprotagonist avatar olleolleolle avatar randallreedjr avatar tekwiz avatar spastorino avatar ioquatix avatar kyledrake avatar winton avatar ilyutov avatar artygus avatar fxn avatar mislav avatar mperham avatar mje113 avatar boone avatar mraidel avatar hexgnu avatar nirvdrum avatar karmi avatar junaruga avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.