]> cat aescling's git repositories - mastodon.git/commitdiff
Preheat status cache
authorEugen Rochko <eugen@zeonfederated.com>
Sun, 5 Feb 2017 16:24:18 +0000 (17:24 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Sun, 5 Feb 2017 16:24:18 +0000 (17:24 +0100)
app/services/warm_cache_service.rb [new file with mode: 0644]
app/workers/distribution_worker.rb
config/environments/development.rb
config/environments/production.rb

diff --git a/app/services/warm_cache_service.rb b/app/services/warm_cache_service.rb
new file mode 100644 (file)
index 0000000..091a471
--- /dev/null
@@ -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
index f423d43ae58df1cccd560153b368e7f6e4c78e03..f4e738d804d7f727b06bb97ac252944a0a388492 100644 (file)
@@ -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
index 51cb43e5dce9e2d9348269fd704de61cf9115dfd..6157f20d35673925c4bc093c3f90c9a107da7f99 100644 (file)
@@ -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
index eaddba522e96b1f73aee5647f50d48aaf8bcf37b..62ea217ef0c4c0935b8dab5fbbfb463a4e090898 100644 (file)
@@ -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.