Merge branch 'standalone_develop' of https://gitlink.org.cn/Trustie/forgeplus into standalone_develop

This commit is contained in:
yystopf 2024-11-19 17:24:43 +08:00
commit a7fd0a5437
1 changed files with 27 additions and 18 deletions

View File

@ -1,22 +1,23 @@
# == Schema Information # == Schema Information
# #
# Table name: edu_settings # Table name: edu_settings
# #
# id :integer not null, primary key # id :integer not null, primary key
# name :string(255) # name :string(255)
# value :string(255) # value :string(255)
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# description :string(255) # description :string(255)
# #
# Indexes # Indexes
# #
# index_edu_settings_on_name (name) UNIQUE # index_edu_settings_on_name (name) UNIQUE
# #
class EduSetting < ApplicationRecord class EduSetting < ApplicationRecord
after_commit :expire_value_cache after_commit :expire_value_cache, on: [:create, :update]
after_commit :clear_value_cache, on: :destroy
scope :by_search, -> (keyword){ where("name LIKE :keyword OR value LIKE :keyword", keyword: "%#{strip_param(keyword)}%") unless strip_param(keyword).blank? } scope :by_search, -> (keyword){ where("name LIKE :keyword OR value LIKE :keyword", keyword: "%#{strip_param(keyword)}%") unless strip_param(keyword).blank? }
@ -25,7 +26,11 @@ class EduSetting < ApplicationRecord
end end
def self.get(key) def self.get(key)
Rails.cache.fetch(value_cache_key(key), expires_in: 1.days) do begin
Rails.cache.fetch(value_cache_key(key), expires_in: 1.days) do
find_by_name(key.to_s)&.value
end
rescue Exception => e
find_by_name(key.to_s)&.value find_by_name(key.to_s)&.value
end end
end end
@ -41,4 +46,8 @@ class EduSetting < ApplicationRecord
def expire_value_cache def expire_value_cache
Rails.cache.write(value_cache_key, value) Rails.cache.write(value_cache_key, value)
end end
def clear_value_cache
Rails.cache.delete(value_cache_key)
end
end end