mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 12:42:57 +08:00
5f64fd0a21
Introduce new patterns for direct sql that are safe and fast. MiniSql is not prone to memory bloat that can happen with direct PG usage. It also has an extremely fast materializer and very a convenient API - DB.exec(sql, *params) => runs sql returns row count - DB.query(sql, *params) => runs sql returns usable objects (not a hash) - DB.query_hash(sql, *params) => runs sql returns an array of hashes - DB.query_single(sql, *params) => runs sql and returns a flat one dimensional array - DB.build(sql) => returns a sql builder See more at: https://github.com/discourse/mini_sql
58 lines
1.4 KiB
Ruby
58 lines
1.4 KiB
Ruby
class StylesheetCache < ActiveRecord::Base
|
|
self.table_name = 'stylesheet_cache'
|
|
|
|
MAX_TO_KEEP = 50
|
|
|
|
def self.add(target, digest, content, source_map)
|
|
old_logger = ActiveRecord::Base.logger
|
|
|
|
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
|
|
.where(target: target)
|
|
.limit(MAX_TO_KEEP)
|
|
.order('id desc')
|
|
.pluck(:id)
|
|
.last
|
|
|
|
DB.exec("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
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: stylesheet_cache
|
|
#
|
|
# id :integer not null, primary key
|
|
# target :string not null
|
|
# digest :string not null
|
|
# content :text not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# theme_id :integer default(-1), not null
|
|
# source_map :text
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_stylesheet_cache_on_target_and_digest (target,digest) UNIQUE
|
|
#
|