mirror of
https://github.com/discourse/discourse.git
synced 2025-06-07 16:46:02 +08:00
PERF: Add scheduled job to delete old stylesheet cache rows (#13747)
This commit is contained in:
11
app/jobs/scheduled/clean_up_stylesheet_cache.rb
Normal file
11
app/jobs/scheduled/clean_up_stylesheet_cache.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Jobs
|
||||||
|
class CleanUpStylesheetCache < ::Jobs::Scheduled
|
||||||
|
every 1.week
|
||||||
|
|
||||||
|
def execute(args)
|
||||||
|
StylesheetCache.clean_up
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -4,6 +4,7 @@ class StylesheetCache < ActiveRecord::Base
|
|||||||
self.table_name = 'stylesheet_cache'
|
self.table_name = 'stylesheet_cache'
|
||||||
|
|
||||||
MAX_TO_KEEP = 50
|
MAX_TO_KEEP = 50
|
||||||
|
CLEANUP_AFTER_DAYS = 150
|
||||||
|
|
||||||
def self.add(target, digest, content, source_map, max_to_keep: nil)
|
def self.add(target, digest, content, source_map, max_to_keep: nil)
|
||||||
max_to_keep ||= MAX_TO_KEEP
|
max_to_keep ||= MAX_TO_KEEP
|
||||||
@ -42,6 +43,10 @@ class StylesheetCache < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.clean_up
|
||||||
|
StylesheetCache.where('created_at < ?', CLEANUP_AFTER_DAYS.days.ago).delete_all
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
@ -4,7 +4,7 @@ require 'rails_helper'
|
|||||||
|
|
||||||
describe StylesheetCache do
|
describe StylesheetCache do
|
||||||
|
|
||||||
describe "add" do
|
describe ".add" do
|
||||||
it "correctly cycles once MAX_TO_KEEP is hit" do
|
it "correctly cycles once MAX_TO_KEEP is hit" do
|
||||||
StylesheetCache.destroy_all
|
StylesheetCache.destroy_all
|
||||||
|
|
||||||
@ -37,6 +37,26 @@ describe StylesheetCache do
|
|||||||
|
|
||||||
expect(StylesheetCache.order(:id).pluck(:target)).to eq(["desktop", "desktop", "mobile", "mobile"])
|
expect(StylesheetCache.order(:id).pluck(:target)).to eq(["desktop", "desktop", "mobile", "mobile"])
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".clean_up" do
|
||||||
|
it "removes items older than threshold" do
|
||||||
|
StylesheetCache.destroy_all
|
||||||
|
|
||||||
|
StylesheetCache.add("a", "b", "c", "map")
|
||||||
|
StylesheetCache.add("d", "e", "f", "map")
|
||||||
|
|
||||||
|
above_threshold = StylesheetCache::CLEANUP_AFTER_DAYS - 1
|
||||||
|
StylesheetCache.first.update!(created_at: above_threshold.days.ago)
|
||||||
|
|
||||||
|
StylesheetCache.clean_up
|
||||||
|
expect(StylesheetCache.all.size).to eq(2)
|
||||||
|
|
||||||
|
below_threshold = StylesheetCache::CLEANUP_AFTER_DAYS + 1
|
||||||
|
StylesheetCache.first.update!(created_at: below_threshold.days.ago)
|
||||||
|
|
||||||
|
StylesheetCache.clean_up
|
||||||
|
expect(StylesheetCache.all.size).to eq(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user