2015-05-05 13:50:13 +08:00
|
|
|
class StylesheetCache < ActiveRecord::Base
|
|
|
|
self.table_name = 'stylesheet_cache'
|
|
|
|
|
2015-05-22 09:21:16 +08:00
|
|
|
MAX_TO_KEEP = 50
|
2015-05-05 13:50:13 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.add(target, digest, content, source_map)
|
2017-08-16 22:59:38 +08:00
|
|
|
old_logger = ActiveRecord::Base.logger
|
2015-05-06 05:12:38 +08:00
|
|
|
|
|
|
|
return false if where(target: target, digest: digest).exists?
|
|
|
|
|
2017-08-16 22:59:38 +08:00
|
|
|
if Rails.env.development?
|
|
|
|
ActiveRecord::Base.logger = nil
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
success = create(target: target, digest: digest, content: content, source_map: source_map)
|
2015-05-05 13:50:13 +08:00
|
|
|
|
|
|
|
count = StylesheetCache.count
|
|
|
|
if count > MAX_TO_KEEP
|
|
|
|
|
2015-05-06 05:33:32 +08:00
|
|
|
remove_lower = StylesheetCache
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(target: target)
|
|
|
|
.limit(MAX_TO_KEEP)
|
|
|
|
.order('id desc')
|
|
|
|
.pluck(:id)
|
|
|
|
.last
|
2015-05-05 13:50:13 +08:00
|
|
|
|
|
|
|
exec_sql("DELETE FROM stylesheet_cache where id < :id", id: remove_lower)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
false
|
2017-08-16 22:59:38 +08:00
|
|
|
ensure
|
|
|
|
if Rails.env.development? && old_logger
|
|
|
|
ActiveRecord::Base.logger = old_logger
|
|
|
|
end
|
2015-05-05 13:50:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-09-18 08:41:10 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: stylesheet_cache
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2017-12-05 23:29:14 +08:00
|
|
|
# target :string(255) not null
|
|
|
|
# digest :string(255) not null
|
2015-09-18 08:41:10 +08:00
|
|
|
# content :text not null
|
2017-12-05 23:29:14 +08:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2017-04-12 22:52:52 +08:00
|
|
|
# theme_id :integer default(-1), not null
|
|
|
|
# source_map :text
|
2015-09-18 08:41:10 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_stylesheet_cache_on_target_and_digest (target,digest) UNIQUE
|
|
|
|
#
|