Coder Social home page Coder Social logo

faraday-manual-cache's Introduction

Faraday::ManualCache

IMPORTANT: This gem hasn't been actively maintained in quite some time, I strongly recommend looking into alternatives!

A super simple Faraday cache implementation.

Unlike faraday-http-cache, faraday-manual-cache ignores cache headers in favor of a manually-specified expires_in.

Installation

Add this line to your application's Gemfile:

gem 'faraday-manual-cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install faraday-manual-cache

Configuration

Create a configuration file and select your memory store class. If you use Rails, just create an initializator.

FaradayManualCache.configure do |config|
  config.memory_store = ActiveSupport::Cache::MemoryStore.new
end

Usage

Super simple example that caches using the default store (MemoryStore) for the default expires_in (30 seconds):

require 'faraday'
require 'faraday-manual-cache'

connection = Faraday.new(url: 'http://example.com') do |builder|
  builder.use :manual_cache
  builder.adapter Faraday.default_adapter
end

The middleware currently takes several options:

  • conditions: Conditional caching (default GET and HEAD requests).
  • cache_key: Key for requests comparison (default URL)
  • expires_in: Cache expiry, in seconds, either as a value or a lambda (default: 30 seconds).
  • logger: Specify a logger to enable logging.

So a more complicated example would be:

require 'faraday'
require 'faraday-manual-cache'

connection = Faraday.new(url: 'http://example.com') do |builder|
  builder.use :manual_cache,
              conditions: ->(env) { env.method == :get },
              cache_key: ->(env) { "prefix-#{env.url}" },
              expires_in: ->(env) { env.status * 10 },
              logger: Rails.logger
  builder.adapter Faraday.default_adapter
end

As with faraday-http-cache it's recommended that faraday-manual-cache be fairly low in the middleware stack.

Contributing

  1. Fork it ( http://github.com/dobs/faraday-manual-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 new Pull Request

Attribution

Some implementation details taken from faraday-http-cache.

Contributors

faraday-manual-cache's People

Contributors

dobs avatar piotrjaworski avatar tldev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

faraday-manual-cache's Issues

Gem Incompatibility Issue with Faraday

Currently i am am on Faraday v1.0.0. When installing this gem in rails there is seems to be a force requirement for Faraday to be v0.9.0. I notice there were some pull request to update the gem version however it seems that those were not successfully built. Is this gem still in support?

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.