Giter Site home page Giter Site logo

grape-cache's Introduction

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

Grape::Cache

Disclaimer: Gem is in early ALPHA

I built this gem for my own neeeds

Installation

Add this line to your application's Gemfile:

gem 'grape-cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grape-cache

Usage

Middleware

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: Grape::Cache::Backend::Redis.new(your_redis_connection)

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) }
end
get :test do

end

Cache key

Response stored in specified backend with cache key generated using:

  • REQUEST_METHOD
  • PATH_INFO
  • 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
end
cache do
  cache_key do
    {user_id: params[:user_id]}
  end
end

get ':company_id/:user_id' do
end

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
end

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
end

At least, they supposed to :D

You can also use prepare block:

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

Gem uses 64bit MurmurHash algo to build hashes

Please, feel free to submit issues or feature requests

Contributing

  1. Fork it ( https://github.com/AlexYankee/grape-cache/fork )
  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

grape-cache's People

Contributors

amar4enko avatar cbillen avatar yurgon 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.