All Projects → sourcelevel → Faraday Http Cache

sourcelevel / Faraday Http Cache

Licence: other
a faraday middleware that respects HTTP cache

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Faraday Http Cache

Miox
Modern infrastructure of complex SPA
Stars: ✭ 374 (+35.51%)
Mutual labels:  middleware, cache
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+246.74%)
Mutual labels:  middleware, cache
Guzzle Cache Middleware
A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.
Stars: ✭ 325 (+17.75%)
Mutual labels:  middleware, cache
Htmlcache
Laravel middleware to cache the rendered html
Stars: ✭ 35 (-87.32%)
Mutual labels:  middleware, cache
Guzzle Advanced Throttle
A Guzzle middleware that can throttle requests according to (multiple) defined rules. It is also possible to define a caching strategy, e.g. get the response from cache when the rate limit is exceeded or always get a cached value to spare your rate limits. Using wildcards in host names is also supported.
Stars: ✭ 120 (-56.52%)
Mutual labels:  middleware, cache
Http Cache
High performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs
Stars: ✭ 184 (-33.33%)
Mutual labels:  middleware, cache
Outputcache
Cache api responses using Redis, Memcached or any cache provider for NodeJS
Stars: ✭ 9 (-96.74%)
Mutual labels:  middleware, cache
Guzzle Cache Middleware
A Guzzle Cache middleware
Stars: ✭ 50 (-81.88%)
Mutual labels:  middleware, cache
Dotweb
Simple and easy go web micro framework
Stars: ✭ 1,354 (+390.58%)
Mutual labels:  middleware, cache
Strapi Middleware Cache
🔌 A cache middleware for https://strapi.io
Stars: ✭ 146 (-47.1%)
Mutual labels:  middleware, cache
express-view-cache
Unobtrusive solution to express framework - cache rendered page, without database requests and rendering.
Stars: ✭ 20 (-92.75%)
Mutual labels:  middleware, cache
Doctrinecachebundle
Symfony2 Bundle for Doctrine Cache
Stars: ✭ 2,813 (+919.2%)
Mutual labels:  cache
Pdfkit
A Ruby gem to transform HTML + CSS into PDFs using the command-line utility wkhtmltopdf
Stars: ✭ 2,799 (+914.13%)
Mutual labels:  middleware
Laravel Demo Mode
A package to protect your work in progress from prying eyes
Stars: ✭ 259 (-6.16%)
Mutual labels:  middleware
Zen
zen is a elegant and lightweight web framework for Go
Stars: ✭ 257 (-6.88%)
Mutual labels:  middleware
Awesome Psr15 Middlewares
A curated list of awesome PSR-15 HTTP Middleware resources
Stars: ✭ 271 (-1.81%)
Mutual labels:  middleware
Web Archives
Browser extension for viewing archived and cached versions of web pages
Stars: ✭ 263 (-4.71%)
Mutual labels:  cache
stats
📊 Request statistics middleware that stores response times, status code counts, etc
Stars: ✭ 15 (-94.57%)
Mutual labels:  middleware
phalcon-authmiddleware
Auth Middleware component for Phalcon.
Stars: ✭ 27 (-90.22%)
Mutual labels:  middleware
cache
A cache for @remark-embedder
Stars: ✭ 12 (-95.65%)
Mutual labels:  cache

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, along with a suitable store to cache the responses. You can use the new shortcut using a symbol or passing the middleware class

client = Faraday.new do |builder|
  builder.use :http_cache, store: Rails.cache
  # or
  builder.use Faraday::HttpCache, store: Rails.cache

  builder.adapter Faraday.default_adapter
end

The middleware accepts a store option for the cache backend responsible for recording the API responses that should be stored. Stores should respond to write, read and delete, just like an object from the ActiveSupport::Cache API.

# Connect the middleware to a Memcache instance.
store = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['localhost:11211'])

client = Faraday.new do |builder|
  builder.use :http_cache, store: store
  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, store: Rails.cache
  builder.adapter Faraday.default_adapter
end

The default store provided is a simple in memory cache that lives on the client instance. This type of store might not be persisted across multiple processes or connection instances so it is probably not suitable for most production environments. Make sure that you configure a store that is suitable for you.

The stdlib JSON module is used for serialization by default, which can struggle with unicode characters in responses. For example, if your JSON returns "name": "Raül" then you might see errors like:

Response could not be serialized: "\xC3" from ASCII-8BIT to UTF-8. Try using Marshal to serialize.

For full unicode support, or if you expect to be dealing with images, you can use Marshal instead. Alternatively you could use another json library like oj or yajl-ruby.

client = Faraday.new do |builder|
  builder.use :http_cache, store: Rails.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, store: Rails.cache, logger: Rails.logger
  builder.adapter Faraday.default_adapter
end

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

Instrumentation

In addition to logging you can instrument the middleware by passing in an :instrumenter option such as ActiveSupport::Notifications (compatible objects are also allowed).

The event http_cache.faraday will be published every time the middleware processes a request. In the event payload, :env contains the response Faraday env and :cache_status contains a Symbol indicating the status of the cache processing for that request:

  • :unacceptable means that the request did not go through the cache at all.
  • :miss means that no cached response could be found.
  • :invalid means that the cached response could not be validated against the server.
  • :valid means that the cached response could be validated against the server.
  • :fresh means that the cached response was still fresh and could be returned without even calling the server.
client = Faraday.new do |builder|
  builder.use :http_cache, store: Rails.cache, instrumenter: ActiveSupport::Notifications
  builder.adapter Faraday.default_adapter
end

# Subscribes to all events from Faraday::HttpCache.
ActiveSupport::Notifications.subscribe "http_cache.faraday" do |*args|
  event = ActiveSupport::Notifications::Event.new(*args)
  cache_status = event.payload[:cache_status]
  statsd = Statsd.new

  case cache_status
  when :fresh, :valid
    statsd.increment('api-calls.cache_hits')
  when :invalid, :miss
    statsd.increment('api-calls.cache_misses')
  when :unacceptable
    statsd.increment('api-calls.cache_bypass')
  end
end

See it live

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

What gets 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 directives are checked.

Shared vs. non-shared caches

By default, the middleware acts as a "shared cache" per RFC 2616. This means it does not cache responses with Cache-Control: private. This behavior can be changed by passing in the :shared_cache configuration option:

client = Faraday.new do |builder|
  builder.use :http_cache, shared_cache: false
  builder.adapter Faraday.default_adapter
end

client.get('http://site/api/some-private-resource') # => will be cached

License

Copyright (c) 2012-2018 Plataformatec. Copyright (c) 2019 SourceLevel and contributors.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].