diff --git a/app/controllers/repertoires_controller.rb b/app/controllers/repertoires_controller.rb new file mode 100644 index 000000000..0ac78a15b --- /dev/null +++ b/app/controllers/repertoires_controller.rb @@ -0,0 +1,5 @@ +class RepertoiresController < ApplicationController + def index + render_ok(repertoires: Repertoire.cache_data) + end +end \ No newline at end of file diff --git a/app/models/repertoire.rb b/app/models/repertoire.rb index 72e1ebfff..140416658 100644 --- a/app/models/repertoire.rb +++ b/app/models/repertoire.rb @@ -3,4 +3,23 @@ class Repertoire < ApplicationRecord has_many :tag_repertoires, through: :sub_repertoires 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 diff --git a/config/routes.rb b/config/routes.rb index 74eaa7bed..d3d04edfb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -661,6 +661,8 @@ Rails.application.routes.draw do get :export_exercises end end + + resources :repertoires, only: [:index] end #git 认证回调