about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-02-05 17:24:18 +0100
committerEugen Rochko <eugen@zeonfederated.com>2017-02-05 17:24:18 +0100
commit19b9e1e2c3d621b5f9f2367980dae62648fe22fa (patch)
tree5753e470b8d797978b0464dbe2c0f88f8b7604c4
parent6fa4e01139a8420d96f9a3c1fcf2b3c5bd0b38a9 (diff)
Preheat status cache
-rw-r--r--app/services/warm_cache_service.rb8
-rw-r--r--app/workers/distribution_worker.rb5
-rw-r--r--config/environments/development.rb5
-rw-r--r--config/environments/production.rb2
4 files changed, 16 insertions, 4 deletions
diff --git a/app/services/warm_cache_service.rb b/app/services/warm_cache_service.rb
new file mode 100644
index 000000000..091a471ff
--- /dev/null
+++ b/app/services/warm_cache_service.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+class WarmCacheService < BaseService
+  def call(cacheable)
+    full_item = cacheable.class.where(id: cacheable.id).with_includes.first
+    Rails.cache.write(cacheable.cache_key, full_item)
+  end
+end
diff --git a/app/workers/distribution_worker.rb b/app/workers/distribution_worker.rb
index f423d43ae..f4e738d80 100644
--- a/app/workers/distribution_worker.rb
+++ b/app/workers/distribution_worker.rb
@@ -4,7 +4,10 @@ class DistributionWorker
   include Sidekiq::Worker
 
   def perform(status_id)
-    FanOutOnWriteService.new.call(Status.find(status_id))
+    status = Status.find(status_id)
+
+    FanOutOnWriteService.new.call(status)
+    WarmCacheService.new.call(status)
   rescue ActiveRecord::RecordNotFound
     true
   end
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 51cb43e5d..6157f20d3 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -20,11 +20,12 @@ Rails.application.configure do
       host: ENV['REDIS_HOST'] || 'localhost',
       port: ENV['REDIS_PORT'] || 6379,
       db: 0,
-      namespace: 'cache'
+      namespace: 'cache',
+      expires_in: 1.minute,
     }
 
     config.public_file_server.headers = {
-      'Cache-Control' => 'public, max-age=172800'
+      'Cache-Control' => 'public, max-age=172800',
     }
   else
     config.action_controller.perform_caching = false
diff --git a/config/environments/production.rb b/config/environments/production.rb
index eaddba522..62ea217ef 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -64,7 +64,7 @@ Rails.application.configure do
     password: ENV.fetch('REDIS_PASSWORD') { false },
     db: 0,
     namespace: 'cache',
-    expires_in: 20.minutes
+    expires_in: 20.minutes,
   }
 
   # Enable serving of images, stylesheets, and JavaScripts from an asset server.