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
|
2015-05-05 01:50:13 -04:00
|
|
|
|
|
|
|
def self.add(target,digest,content)
|
2015-05-05 17:12:38 -04:00
|
|
|
|
|
|
|
return false if where(target: target, digest: digest).exists?
|
|
|
|
|
2015-05-05 01:50:13 -04:00
|
|
|
success = create(target: target, digest: digest, content: content)
|
|
|
|
|
|
|
|
count = StylesheetCache.count
|
|
|
|
if count > MAX_TO_KEEP
|
|
|
|
|
2015-05-05 17:33:32 -04:00
|
|
|
remove_lower = StylesheetCache
|
|
|
|
.where(target: target)
|
|
|
|
.limit(MAX_TO_KEEP)
|
2015-05-05 01:50:13 -04:00
|
|
|
.order('id desc')
|
|
|
|
.pluck(:id)
|
|
|
|
.last
|
|
|
|
|
|
|
|
exec_sql("DELETE FROM stylesheet_cache where id < :id", id: remove_lower)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|