Giter Club home page Giter Club logo

another_grape_cache's Introduction

AnotherGrapeCache

Here is yet another implementation for Grape cache. This implementation based on middlewares, a standard extension interface of Grape. Response body cached effectively on response status 200 in a format of API.

Installation

Add this line to your application's Gemfile:

gem 'another_grape_cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install another_grape_cache

Usage

Initializer

    # config/another_grape_cache.rb
    
    AnotherGrapeCache.configure do |config|
      config.cache_backend = Rails.cache
      config.perform_caching = true
    end
  • cache_backend - should implement a public interface of read/write/exists? as a standard Rails.cache
  • perform_caching - toggles caching (might be disabled for dev)

Inside Endpoint

    # app/api/posts/index_endpoint.rb
    
    include AnotherGrapeCache::CacheHelper

    helpers do
      def posts
        @posts ||= Post.limit(10)
      end
    end

    cache(max_age: 10.minutes, expires_in: 5.minutes, private_cache: true, cache_if: -> { rand(2) == 1 }) do
      posts.cache_key
    end
    
    get do
      present posts
    end

Options

  • max_age - ActiveSupport time range, max cache time for client. Max age set as directive of the HTTP header Cache-Control, default 0
  • private_cache - Boolean, set private/public for the HTTP header Cache-Control, default false.
  • expires_in - parameter for caching backend (max time to live).
  • cache_if - a lambda which must return a Boolean value. The lambda is executed in the context of an endpoint
  • block - should return a value for cache_key. This block is executed inside a route context, helpers are available there

Cache hit/miss status exposed as a HTTP header X-Cache-Status: MISS/HIT

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/SamMolokanov/another_grape_cache. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the AnotherGrapeCache project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

another_grape_cache's People

Contributors

dependabot[bot] avatar sammolokanov avatar

Stargazers

 avatar

Watchers

 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.