Browse Source

Preheat status cache

closed-social-glitch-2
Eugen Rochko 7 years ago
parent
commit
19b9e1e2c3
4 changed files with 16 additions and 4 deletions
  1. +8
    -0
      app/services/warm_cache_service.rb
  2. +4
    -1
      app/workers/distribution_worker.rb
  3. +3
    -2
      config/environments/development.rb
  4. +1
    -1
      config/environments/production.rb

+ 8
- 0
app/services/warm_cache_service.rb View File

@ -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

+ 4
- 1
app/workers/distribution_worker.rb View 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

+ 3
- 2
config/environments/development.rb View 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

+ 1
- 1
config/environments/production.rb View 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.

Loading…
Cancel
Save