去掉缓存

dev_forum
cxt 5 years ago
parent 9feb0d3869
commit 88e839b48d

@ -1,6 +1,6 @@
class DepartmentsController < ApplicationController class DepartmentsController < ApplicationController
def for_option def for_option
render_ok(departments: Department.cached_names_data(current_school)) render_ok(departments: current_school.departments.select(:id, :name).as_json)
end end
private private

@ -1,5 +1,5 @@
class RepertoiresController < ApplicationController class RepertoiresController < ApplicationController
def index def index
render_ok(repertoires: Repertoire.cache_data) render_ok(repertoires: Repertoire.select(:id, :name).order(:created_at).as_json)
end end
end end

@ -11,6 +11,6 @@ class SchoolsController < ApplicationController
end end
def for_option def for_option
render_ok(schools: School.cached_names_data) render_ok(schools: School.select(:id, :name).as_json)
end end
end end

@ -2,23 +2,4 @@ class Department < ApplicationRecord
belongs_to :school belongs_to :school
has_many :department_members, dependent: :destroy has_many :department_members, dependent: :destroy
after_commit :reset_data_cache
def self.cached_names_data(school)
Rails.cache.fetch(names_data_cache_key(school.id), expires_in: 7.days) do
school.departments.select(:id, :name).as_json
end
end
private
def reset_data_cache
# 清除部门名称缓存
Rails.cache.delete(self.class.names_data_cache_key(school_id))
end
def self.names_data_cache_key(school_id)
"schools/#{school_id}/department_names_data"
end
end end

@ -3,23 +3,4 @@ class Repertoire < ApplicationRecord
has_many :tag_repertoires, through: :sub_repertoires has_many :tag_repertoires, through: :sub_repertoires
has_many :user_interests, dependent: :delete_all has_many :user_interests, dependent: :delete_all
after_create_commit :reset_cache_data
after_update_commit :reset_cache_data
def self.cache_data
Rails.cache.fetch(data_cache_key, expires_in: 1.days) do
Repertoire.select(:id, :name).order(:created_at).as_json
end
end
def self.data_cache_key
'repertoire/cache_data'
end
private
def reset_cache_data
Rails.cache.delete(self.class.data_cache_key)
end
end end

@ -10,9 +10,6 @@ class School < ApplicationRecord
has_many :ec_major_schools, :dependent => :destroy has_many :ec_major_schools, :dependent => :destroy
has_many :ec_majors, :through => :ec_major_schools has_many :ec_majors, :through => :ec_major_schools
after_create_commit :reset_data_cache
after_update_commit :reset_data_cache
# 学校管理员 # 学校管理员
def manager?(user) def manager?(user)
ec_school_users.exists?(user_id: user.id) ec_school_users.exists?(user_id: user.id)
@ -33,23 +30,4 @@ class School < ApplicationRecord
def manage_permission?(user) def manage_permission?(user)
manager?(user) || major_manager?(user) || course_manager?(user) manager?(user) || major_manager?(user) || course_manager?(user)
end end
def self.cached_names_data
Rails.cache.fetch(names_data_cache_key, expires_in: 7.days) do
School.select(:id, :name).as_json
end
end
private
def reset_data_cache
# 清除学校名称缓存
if new_record? || name_previously_changed?
Rails.cache.delete(self.class.names_data_cache_key)
end
end
def self.names_data_cache_key
'schools/names_data'
end
end end

Loading…
Cancel
Save