about summary refs log tree commit diff
path: root/app/controllers/concerns
diff options
context:
space:
mode:
authormultiple creatures <multiple_creatures@monsterware.dev>2020-01-17 10:36:42 +0000
committermultiple creatures <multiple_creatures@monsterware.dev>2020-01-17 10:36:42 +0000
commitd2acdf2ea68dce252e827041d226d4402369669b (patch)
treec4c8acbd7b1b745aadeb60547639f341fd7d7da8 /app/controllers/concerns
parent753d06495189dd6ffd95ea91028cc2a03866e974 (diff)
parent7b4381b4bdd2e3535b907abfe5f5ffa702d692aa (diff)
Merge pull request 'Port CacheConcern from upstream to define render_with_cache' (#13) from blueraccoon/port-cacheconcern into master
Reviewed-on: https://monsterware.dev/monsterpit/mastodon/pulls/13
Diffstat (limited to 'app/controllers/concerns')
-rw-r--r--app/controllers/concerns/cache_concern.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/app/controllers/concerns/cache_concern.rb b/app/controllers/concerns/cache_concern.rb
new file mode 100644
index 000000000..90d7d26db
--- /dev/null
+++ b/app/controllers/concerns/cache_concern.rb
@@ -0,0 +1,52 @@
+# 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
+# TODO: Uncomment the following line if and when public_fetch_mode exists
+#    response.headers['Vary'] = public_fetch_mode? ? 'Accept' : 'Accept, Signature'
+    response.headers['Vary'] = 'Accept'
+  end
+
+  def cache_collection(raw, klass)
+    return raw unless klass.respond_to?(:with_includes)
+
+    raw                    = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
+    cached_keys_with_value = Rails.cache.read_multi(*raw).transform_keys(&:id)
+    uncached_ids           = raw.map(&:id) - cached_keys_with_value.keys
+
+    klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
+
+    unless uncached_ids.empty?
+      uncached = klass.where(id: uncached_ids).with_includes.each_with_object({}) { |item, h| h[item.id] = item }
+
+      uncached.each_value do |item|
+        Rails.cache.write(item, item)
+      end
+    end
+
+    raw.map { |item| cached_keys_with_value[item.id] || uncached[item.id] }.compact
+  end
+end