discourse/app/models/stylesheet_cache.rb

58 lines
1.4 KiB
Ruby
Raw Normal View History

class StylesheetCache < ActiveRecord::Base
self.table_name = 'stylesheet_cache'
MAX_TO_KEEP = 50
2017-07-27 21:20:09 -04:00
def self.add(target, digest, content, source_map)
old_logger = ActiveRecord::Base.logger
2015-05-05 17:12:38 -04: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 21:20:09 -04:00
.where(target: target)
.limit(MAX_TO_KEEP)
.order('id desc')
.pluck(:id)
.last
exec_sql("DELETE FROM stylesheet_cache where id < :id", id: remove_lower)
end
success
rescue ActiveRecord::RecordNotUnique
false
ensure
if Rails.env.development? && old_logger
ActiveRecord::Base.logger = old_logger
end
end
end
2015-09-17 20:41:10 -04:00
# == Schema Information
#
# Table name: stylesheet_cache
#
# id :integer not null, primary key
2016-02-22 18:33:53 -05:00
# target :string not null
# digest :string not null
2015-09-17 20:41:10 -04:00
# content :text not null
# created_at :datetime not null
# updated_at :datetime not null
# 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
#