Giter Club home page Giter Club logo

faraday-http-cache's Introduction

Faraday Http Cache

Build Status

a Faraday middleware that respects HTTP cache, by checking expiration and validation of the stored responses.

Installation

Add it to your Gemfile:

gem 'faraday-http-cache'

Usage and configuration

You have to use the middleware in the Faraday instance that you want to. You can use the new shortcut using a symbol or passing the middleware class

client = Faraday.new do |builder|
  builder.use :http_cache
  # or
  builder.use Faraday::HttpCache

  builder.adapter Faraday.default_adapter
end

The middleware uses the ActiveSupport::Cache API to record the responses from the targeted endpoints, and any extra configuration option will be used to setup the cache store.

# Connect the middleware to a Memcache instance.
client = Faraday.new do |builder|
  builder.use :http_cache, :mem_cache_store, 'localhost:11211'
  builder.adapter Faraday.default_adapter
end

# Or use the Rails.cache instance inside your Rails app.
client = Faraday.new do |builder|
  builder.use :http_cache, Rails.cache
  builder.adapter Faraday.default_adapter
end

The default store provided by ActiveSupport is the MemoryStore one, so it's important to configure a proper one for your production environment.

MultiJson is used for serialization by default. If you expect to be dealing with images, you can use Marshal instead.

client = Faraday.new do |builder|
  builder.use :http_cache, serializer: Marshal
  builder.adapter Faraday.default_adapter
end

Logging

You can provide a :logger option that will be receive debug informations based on the middleware operations:

client = Faraday.new do |builder|
  builder.use :http_cache, logger: Rails.logger
  builder.adapter Faraday.default_adapter
end

client.get('http://site/api/users')
# logs "HTTP Cache: [GET users] miss, store"

See it live

You can clone this repository, install it's dependencies with Bundler (run bundle install) and execute the files under the examples directory to see a sample of the middleware usage.

What get's cached?

The middleware will use the following headers to make caching decisions:

  • Cache-Control
  • Age
  • Last-Modified
  • ETag
  • Expires

Cache-Control

The max-age, must-revalidate, proxy-revalidate and s-maxage are checked.

Note: private caches are ignored.

License

Copyright (c) 2012-2013 Plataformatec. See LICENSE file.

faraday-http-cache's People

Contributors

alex-handley avatar dahlb avatar erichkist avatar erichmachado avatar lucasmazza avatar rafaelfranca 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.