Git Product home page Git Product logo

redis-rack-cache's Introduction

Redis stores for Rack::Cache

Build Status Gem Version Build Status Code Climate

redis-rack-cache provides a Redis backed store for Rack::Cache, an HTTP cache. See the main redis-store readme for general guidelines.

NOTE: This gem is necessary in addition to redis-rails if you use Redis to store the Rails cache. redis-rails does not pull in this gem by default since not all applications use Rack::Cache as their HTTP cache.

Installation

# Gemfile
gem 'redis-rack-cache'

Usage

In a Rails app, you can configure your Rack::Cache stores like this:

# config/environments/production.rb
Rails.application.configure do
  config.action_dispatch.rack_cache = {
    metastore: "#{Rails.credentials.redis_url}/1/rack_cache_metastore",
    entitystore: "#{Rails.credentials.redis_url}/1/rack_cache_entitystore"
    # NOTE: `:meta_store` and `:entity_store` are also supported.
  }
end

For more complicated setups, like when using custom options, the following syntax can also be used:

# config/environments/production.rb
Rails.application.configure do
  config.action_dispatch.rack_cache = {
    meta_store: ::Rack::Cache::MetaStore::Redis.new("#{Rails.credentials.redis_url}/1/rack_cache_metastore", default_ttl: 10.days.to_i),
    entity_store: ::Rack::Cache::EntityStore::Redis.new("#{Rails.credentials.redis_url}/1/rack_cache_entitystore", default_ttl: 120.days.to_i)
    # NOTE: `:metastore` and `:entitystore` are also supported.
  }
end

For standalone usage (in non-Rails apps):

# config.ru
require 'rack'
require 'rack/cache'
require 'redis-rack-cache'

use Rack::Cache,
  metastore: 'redis://localhost:6379/0/metastore',
  entitystore: 'redis://localhost:6380/0/entitystore'

Compression

Redis::Rack::Cache supports data compression for entities over 1K when transmitting back and forth over the wire to your Redis server. Compressing data can improve bandwidth usage as well as RAM/storage consumption, and is recommended if you operate a large-scale Rails application.

To enable this feature, pass the :compress option when configuring Rack::Cache:

Rails.application.configure do
  config.action_dispatch.rack_cache = {
    metastore: "#{Rails.credentials.redis_url}/1",
    entitystore: "#{Rails.credentials.redis_url}/2",
    compress: true
  }
end

If compression is turned on, but no driver has been selected, Redis::Rack::Cache will use Ruby's internal ZLib integrations and compress entities with GZip. You can specify :deflate if you want to use the deflate algorithm, :gzip if you want to be specific about it, or a custom object that responds to .deflate(data) and .inflate(data) to compress/decompress data, respectively. For example, you can use Google's Snappy for ludicrous-speed compression and decompression like this:

Rails.application.configure do
  config.action_dispatch.rack_cache = {
    metastore: "#{Rails.credentials.redis_url}/1",
    entitystore: "#{Rails.credentials.redis_url}/2",
    compress: Snappy
  }
end

NOTE: Since metadata would have to be marshalled before compression in order to rehydrate it back into an object, only data stored in the EntityStore is compressed for now. We'd love your feedback though, let us know if there's a good use case for MetaStore compression!

Development

First, get the project set up on your local machine:

git clone https://github.com/redis-store/redis-rack-cache.git
cd redis-rack-cache
bundle install

You can run the following command to run the test suite:

bundle exec rake test

If you are on Snow Leopard you have to run env ARCHFLAGS="-arch x86_64" bundle exec rake

Copyright

2009 - 2013 Luca Guidi - http://lucaguidi.com, released under the MIT license

redis-rack-cache's People

Contributors

tubbo avatar mezis avatar jodosha avatar marceloboeira avatar radar avatar olleolleolle avatar shiro16 avatar skull-squadron avatar manuelmeurer avatar mathieujobin avatar yasyf 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.