Skip to content

A simple request cache strategy for Grape applications.

Notifications You must be signed in to change notification settings

gabrielcorado/grape_cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grape Cache Build Status Gem Version

+-----------+     Cached?
|  Request  +------------------+
+-----------+                  |
                               |
                               |
                               v
+-----------+     No     +-----+-----+
| API Call  +<-----------+   Cache   |
+-----+-----+            +-----+-----+
      |                        |
      |                        |
      v                        |
+-----+-----+     Yes          |
| Response  +<-----------------+
+-----------+

How it works

The module generates a key based on the Grape Route(METHOD + PATH) and the current params (HTTP BODY + QUERY STRINGS) and set the result of the API Call to this key. When another request with exacly same attributes that will generate the same key again, it does not call the API again, it just return the last response.

Usage

# Add the Middleware into your API class
# In the middleware definition you have to specify the
# store used by the middleware, also you could pass the
# params for this store like `:expires_in`
class Application < Grape::API
  use GrapeCache, store: Rails.cache, options: { expires_in: 1.hour }
end

# After this just simply set your routes with a cache flag
get '/hello/cache', cache: true do
  { hello: 'From cache' }
end

# To use it with a shared cache env you could use the flag `:namespace`
# to define it. In this case I'll use the Apartment as example.
use GrapeCache, store: Rails.cache, namespace: -> { Apartment::Tenant.current }

Development

  • Building the docker container: docker build -t grape-cache .
  • Running the tests:
    • With volume: docker run --rm -it -v (PWD):/app grape-cache bundle exec rspec
    • Without volume: docker run --rm -it grape-cache bundle exec rspec

About

A simple request cache strategy for Grape applications.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages