Module: WCC::JsonAPI::ApiActionCaching
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/wcc/jsonapi/concerns/api_action_caching.rb
Overview
This concern enables ActionCaching for our JsonAPI endpoints. It improves on the standard actionpack-action_caching gem for our specific use case. More specifically, it accomplishes the following:
-
If the incoming request is a conditional GET with ETag or Last-Modified, it
returns 304 not modified without invoking the controller or loading the resource -
If the incoming request is stale, it will load the rendered JSON and caching
headers from the cache store without invoking the action -
If the incoming request is stale AND the CacheVersion for the specified key is old,
it invokes the action and saves both the rendered JSON response and the caching headers.
It is very important to use stale? or fresh_when? to set the caching headers inside the action, otherwise this concern cannot respond to conditional GET requests.
The controller must also include ActionController::Caching (or extend from Api::V1::BaseController which includes it)
Example: class Api::V1::MyController < Api::V1::BaseController
include Api::V1::ApiActionCaching
caches_api_action :show, version_key: :my_resource # see CacheVersion::KEY_MAPPING
def show
@resource = load_resource
@page = Api::V1::MyResourceSerializer.new(@person)
render json: @page if stale?(
strong_etag: @page.etag, last_modified: @page.last_modified, public: true,
)
end
Modeled after github.com/rails/actionpack-action_caching/blob/v1.2.0/lib/action_controller/caching/actions.rb
Defined Under Namespace
Classes: ApiActionCacheFilter