discourse/app/models/stylesheet_cache.rb

62 lines
1.5 KiB
Ruby
Raw Normal View History

class StylesheetCache < ActiveRecord::Base
self.table_name = 'stylesheet_cache'
MAX_TO_KEEP = 50
def self.add(target, digest, content, source_map, max_to_keep: nil)
max_to_keep ||= MAX_TO_KEEP
old_logger = ActiveRecord::Base.logger
2015-05-05 16:12:38 -05:00
return false if where(target: target, digest: digest).exists?
if Rails.env.development?
ActiveRecord::Base.logger = nil
end
success = create(target: target, digest: digest, content: content, source_map: source_map)
count = StylesheetCache.count
if count > max_to_keep
remove_lower = StylesheetCache
2017-07-27 20:20:09 -05:00
.where(target: target)
.limit(max_to_keep)
2017-07-27 20:20:09 -05:00
.order('id desc')
.pluck(:id)
.last
DB.exec(<<~SQL, id: remove_lower, target: target)
DELETE FROM stylesheet_cache
WHERE id < :id AND target = :target
SQL
end
success
rescue ActiveRecord::RecordNotUnique
false
ensure
if Rails.env.development? && old_logger
ActiveRecord::Base.logger = old_logger
end
end
end
2015-09-17 19:41:10 -05:00
# == Schema Information
#
# Table name: stylesheet_cache
#
# id :integer not null, primary key
2019-01-11 11:19:23 -06:00
# target :string(255) not null
# digest :string(255) not null
2015-09-17 19:41:10 -05:00
# content :text not null
2019-01-11 11:19:23 -06:00
# created_at :datetime
# updated_at :datetime
# theme_id :integer default(-1), not null
# source_map :text
2015-09-17 19:41:10 -05:00
#
# Indexes
#
# index_stylesheet_cache_on_target_and_digest (target,digest) UNIQUE
#