@@ -10,6 +10,7 @@ class ApplicationController < ActionController::Base
1010 include Localized
1111 include UserTrackingConcern
1212 include SessionTrackingConcern
13+ include CacheConcern
1314
1415 helper_method :current_account
1516 helper_method :current_session
@@ -115,47 +116,10 @@ def current_theme
115116 current_user . setting_theme
116117 end
117118
118- def cache_collection ( raw , klass )
119- return raw unless klass . respond_to? ( :with_includes )
120-
121- raw = raw . cache_ids . to_a if raw . is_a? ( ActiveRecord ::Relation )
122- cached_keys_with_value = Rails . cache . read_multi ( *raw ) . transform_keys ( &:id )
123- uncached_ids = raw . map ( &:id ) - cached_keys_with_value . keys
124-
125- klass . reload_stale_associations! ( cached_keys_with_value . values ) if klass . respond_to? ( :reload_stale_associations! )
126-
127- unless uncached_ids . empty?
128- uncached = klass . where ( id : uncached_ids ) . with_includes . each_with_object ( { } ) { |item , h | h [ item . id ] = item }
129-
130- uncached . each_value do |item |
131- Rails . cache . write ( item , item )
132- end
133- end
134-
135- raw . map { |item | cached_keys_with_value [ item . id ] || uncached [ item . id ] } . compact
136- end
137-
138119 def respond_with_error ( code )
139120 respond_to do |format |
140121 format . any { head code }
141122 format . html { render "errors/#{ code } " , layout : 'error' , status : code }
142123 end
143124 end
144-
145- def render_cached_json ( cache_key , **options )
146- options [ :expires_in ] ||= 3 . minutes
147- cache_public = options . key? ( :public ) ? options . delete ( :public ) : true
148- content_type = options . delete ( :content_type ) || 'application/json'
149-
150- data = Rails . cache . fetch ( cache_key , { raw : true } . merge ( options ) ) do
151- yield . to_json
152- end
153-
154- expires_in options [ :expires_in ] , public : cache_public
155- render json : data , content_type : content_type
156- end
157-
158- def set_cache_headers
159- response . headers [ 'Vary' ] = public_fetch_mode? ? 'Accept' : 'Accept, Signature'
160- end
161125end
0 commit comments