2019-07-21 22:32:16 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module CacheConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def render_with_cache(**options)
|
|
|
|
raise ArgumentError, 'only JSON render calls are supported' unless options.key?(:json) || block_given?
|
|
|
|
|
|
|
|
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 set_cache_headers
|
|
|
|
response.headers['Vary'] = public_fetch_mode? ? 'Accept' : 'Accept, Signature'
|
|
|
|
end
|
|
|
|
|
2024-04-08 15:46:13 +02:00
|
|
|
# TODO: Rename this method, as it does not perform any caching anymore.
|
2019-07-21 22:32:16 +02:00
|
|
|
def cache_collection(raw, klass)
|
2024-04-08 15:46:13 +02:00
|
|
|
return raw unless klass.respond_to?(:preload_cacheable_associations)
|
2019-07-21 22:32:16 +02:00
|
|
|
|
2024-04-08 15:46:13 +02:00
|
|
|
records = raw.to_a
|
2021-03-18 00:41:32 +01:00
|
|
|
|
2024-04-08 15:46:13 +02:00
|
|
|
klass.preload_cacheable_associations(records)
|
2019-07-21 22:32:16 +02:00
|
|
|
|
2024-04-08 15:46:13 +02:00
|
|
|
records
|
2019-07-21 22:32:16 +02:00
|
|
|
end
|
2020-08-28 12:31:56 +02:00
|
|
|
|
2024-04-08 15:46:13 +02:00
|
|
|
# TODO: Rename this method, as it does not perform any caching anymore.
|
2020-08-28 12:31:56 +02:00
|
|
|
def cache_collection_paginated_by_id(raw, klass, limit, options)
|
2024-04-08 15:46:13 +02:00
|
|
|
cache_collection raw.to_a_paginated_by_id(limit, options), klass
|
2020-08-28 12:31:56 +02:00
|
|
|
end
|
2019-07-21 22:32:16 +02:00
|
|
|
end
|