Skip to content
HTTP and application caching for Grape framework
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/grape Updated for Grape 0.18 Dec 12, 2016
spec Updated for Grape 0.18 Dec 12, 2016
.gitignore Init Oct 20, 2014
.ruby-gemset Init Oct 20, 2014
.ruby-version Updated for Grape 0.18 Dec 12, 2016
Gemfile Updated for Grape 0.18 Dec 12, 2016
Guardfile Init Oct 20, 2014
LICENSE.txt Init Oct 20, 2014 Update Dec 29, 2016
Rakefile Updated for Grape 0.18 Dec 12, 2016
grape-cache.gemspec Updated for Grape 0.18 Dec 12, 2016

Important notice !

Since i'm not Ruby developer anymore i 'd like to transfer this repo to someone who is interested in developing and supporting it. Ping me via issues or pull request if it's you :D


Disclaimer: Gem is in early ALPHA

I built this gem for my own neeeds


Add this line to your application's Gemfile:

gem 'grape-cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grape-cache



Include Grape::Cache::Middleware in your app stack ABOVE your Grape app. Example:

use Grape::Cache::Middleware
run Your::Grape::App

You can use Redis backend for caching middleware as follows:

use Grape::Cache::Middleware, backend:

Route caching

As simple as follows:

cache do
  expire_after 5.seconds
  etag { #Your etag generation code }
  last_modified { #Your last_modified code here (expecting something what can receive #httpdate) }
get :test do


Cache key

Response stored in specified backend with cache key generated using:

  • Accept-Version header
  • hash built from route declared parameters

You can override hash source with cache_key block:

params do
  requires company_id, type: Integer
  requires user_id, type: Integer
cache do
  cache_key do
    {user_id: params[:user_id]}

get ':company_id/:user_id' do

Cache expiration time

You can specify cache expiration time in two ways, class-eval or runtime-eval

cache do
  expire_after 5.seconds # For class-eval specify time offset
  expire_after { 10.seconds.from_now } # For runtime-eval specify datetime

Cache expiration time used in Cache-Control header for max-age section and for in-app cache expiration

Useful info

All configuration blocks executed against current endpoint:

etag do
  # Use params, env here

At least, they supposed to :D

You can also use prepare block:

cache do
  prepare { @user = User.find_by(params[:id]) }
  etag { @user.etag }
get :user do
  present @user

Gem uses 64bit MurmurHash algo to build hashes

Please, feel free to submit issues or feature requests


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
You can’t perform that action at this time.