2019-07-21 13:32:16 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module CacheConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2023-04-19 07:07:29 -07:00
|
|
|
class_methods do
|
2023-04-26 02:42:47 -07:00
|
|
|
def vary_by(value, **kwargs)
|
|
|
|
before_action(**kwargs) do |controller|
|
2023-04-19 07:07:29 -07:00
|
|
|
response.headers['Vary'] = value.respond_to?(:call) ? controller.instance_exec(&value) : value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-23 13:27:24 -07:00
|
|
|
included do
|
|
|
|
after_action :enforce_cache_control!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prevents high-entropy headers such as `Cookie`, `Signature` or `Authorization`
|
|
|
|
# from being used as cache keys, while allowing to `Vary` on them (to not serve
|
|
|
|
# anonymous cached data to authenticated requests when authentication matters)
|
|
|
|
def enforce_cache_control!
|
|
|
|
vary = response.headers['Vary']&.split&.map { |x| x.strip.downcase }
|
|
|
|
return unless vary.present? && %w(cookie authorization signature).any? { |header| vary.include?(header) && request.headers[header].present? }
|
|
|
|
|
|
|
|
response.cache_control.replace(private: true, no_store: true)
|
|
|
|
end
|
|
|
|
|
2019-07-21 13:32:16 -07:00
|
|
|
def render_with_cache(**options)
|
2023-04-19 07:07:29 -07:00
|
|
|
raise ArgumentError, 'Only JSON render calls are supported' unless options.key?(:json) || block_given?
|
2019-07-21 13:32:16 -07:00
|
|
|
|
|
|
|
key = options.delete(:key) || [[params[:controller], params[:action]].join('/'), options[:json].respond_to?(:cache_key) ? options[:json].cache_key : nil, options[:fields].nil? ? nil : options[:fields].join(',')].compact.join(':')
|
|
|
|
expires_in = options.delete(:expires_in) || 3.minutes
|
|
|
|
body = Rails.cache.read(key, raw: true)
|
|
|
|
|
|
|
|
if body
|
|
|
|
render(options.except(:json, :serializer, :each_serializer, :adapter, :fields).merge(json: body))
|
|
|
|
else
|
|
|
|
if block_given?
|
|
|
|
options[:json] = yield
|
|
|
|
elsif options[:json].is_a?(Symbol)
|
|
|
|
options[:json] = send(options[:json])
|
|
|
|
end
|
|
|
|
|
|
|
|
render(options)
|
|
|
|
Rails.cache.write(key, response.body, expires_in: expires_in, raw: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_collection(raw, klass)
|
|
|
|
return raw unless klass.respond_to?(:with_includes)
|
|
|
|
|
2021-03-17 16:41:32 -07:00
|
|
|
raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
|
|
|
|
return [] if raw.empty?
|
|
|
|
|
2024-01-05 13:57:47 -08:00
|
|
|
cached_keys_with_value = Rails.cache.read_multi(*raw).transform_keys(&:id)
|
2023-03-17 06:37:30 -07:00
|
|
|
|
|
|
|
uncached_ids = raw.map(&:id) - cached_keys_with_value.keys
|
2019-07-21 13:32:16 -07:00
|
|
|
|
|
|
|
klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
|
|
|
|
|
|
|
|
unless uncached_ids.empty?
|
2021-01-12 00:27:38 -08:00
|
|
|
uncached = klass.where(id: uncached_ids).with_includes.index_by(&:id)
|
2024-01-05 13:57:47 -08:00
|
|
|
Rails.cache.write_multi(uncached.values.to_h { |i| [i, i] })
|
2019-07-21 13:32:16 -07:00
|
|
|
end
|
|
|
|
|
2021-01-09 15:32:01 -08:00
|
|
|
raw.filter_map { |item| cached_keys_with_value[item.id] || uncached[item.id] }
|
2019-07-21 13:32:16 -07:00
|
|
|
end
|
2020-08-28 03:31:56 -07:00
|
|
|
|
|
|
|
def cache_collection_paginated_by_id(raw, klass, limit, options)
|
2020-08-31 03:47:09 -07:00
|
|
|
cache_collection raw.cache_ids.to_a_paginated_by_id(limit, options), klass
|
2020-08-28 03:31:56 -07:00
|
|
|
end
|
2019-07-21 13:32:16 -07:00
|
|
|
end
|