2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-05-05 01:50:13 -04:00
|
|
|
class StylesheetCache < ActiveRecord::Base
|
|
|
|
self.table_name = 'stylesheet_cache'
|
|
|
|
|
2015-05-21 21:21:16 -04:00
|
|
|
MAX_TO_KEEP = 50
|
2021-07-16 10:58:01 -04:00
|
|
|
CLEANUP_AFTER_DAYS = 150
|
2015-05-05 01:50:13 -04:00
|
|
|
|
2018-09-13 22:54:11 -04:00
|
|
|
def self.add(target, digest, content, source_map, max_to_keep: nil)
|
|
|
|
max_to_keep ||= MAX_TO_KEEP
|
2017-08-16 10:59:38 -04:00
|
|
|
old_logger = ActiveRecord::Base.logger
|
2015-05-05 17:12:38 -04:00
|
|
|
|
|
|
|
return false if where(target: target, digest: digest).exists?
|
|
|
|
|
2017-08-16 10:59:38 -04:00
|
|
|
if Rails.env.development?
|
|
|
|
ActiveRecord::Base.logger = nil
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
success = create(target: target, digest: digest, content: content, source_map: source_map)
|
2015-05-05 01:50:13 -04:00
|
|
|
|
|
|
|
count = StylesheetCache.count
|
2018-09-13 22:54:11 -04:00
|
|
|
if count > max_to_keep
|
2015-05-05 01:50:13 -04:00
|
|
|
|
2015-05-05 17:33:32 -04:00
|
|
|
remove_lower = StylesheetCache
|
|
|
|
.where(target: target)
|
2018-09-13 22:54:11 -04:00
|
|
|
.limit(max_to_keep)
|
2015-05-05 01:50:13 -04:00
|
|
|
.order('id desc')
|
|
|
|
.pluck(:id)
|
|
|
|
.last
|
|
|
|
|
2018-09-13 22:54:11 -04:00
|
|
|
DB.exec(<<~SQL, id: remove_lower, target: target)
|
|
|
|
DELETE FROM stylesheet_cache
|
|
|
|
WHERE id < :id AND target = :target
|
|
|
|
SQL
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
success
|
2020-07-21 00:15:28 -04:00
|
|
|
rescue ActiveRecord::RecordNotUnique, ActiveRecord::ReadOnlyError
|
2015-05-05 01:50:13 -04:00
|
|
|
false
|
2017-08-16 10:59:38 -04:00
|
|
|
ensure
|
|
|
|
if Rails.env.development? && old_logger
|
|
|
|
ActiveRecord::Base.logger = old_logger
|
|
|
|
end
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
|
|
|
|
2021-07-16 10:58:01 -04:00
|
|
|
def self.clean_up
|
|
|
|
StylesheetCache.where('created_at < ?', CLEANUP_AFTER_DAYS.days.ago).delete_all
|
|
|
|
end
|
|
|
|
|
2015-05-05 01:50:13 -04:00
|
|
|
end
|
2015-09-17 20:41:10 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: stylesheet_cache
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2019-01-11 14:29:56 -05:00
|
|
|
# target :string not null
|
|
|
|
# digest :string not null
|
2015-09-17 20:41:10 -04:00
|
|
|
# content :text not null
|
2019-01-11 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-04-12 10:52:52 -04:00
|
|
|
# theme_id :integer default(-1), not null
|
|
|
|
# source_map :text
|
2015-09-17 20:41:10 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_stylesheet_cache_on_target_and_digest (target,digest) UNIQUE
|
|
|
|
#
|