Module: ActiveModel::Serializer::Caching
- Extended by:
- ActiveSupport::Concern
- Included in:
- ActiveModel::Serializer
- Defined in:
- lib/active_model/serializer/concerns/caching.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CALLER_FILE =
Matches
"c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'" AND "/c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'" AS c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb / \A # start of string .+ # file path (one or more characters) (?= # stop previous match when :\d+ # a colon is followed by one or more digits :in # followed by a colon followed by in ) /x
Instance Method Summary collapse
- #cache_key(adapter_instance) ⇒ Object
- #expand_cache_key(parts) ⇒ Object
- #fetch(adapter_instance, cache_options = serializer_class._cache_options, key = nil) ⇒ Object
-
#fetch_attributes(fields, cached_attributes, adapter_instance) ⇒ Object
INSTANCE METHODS.
-
#fetch_attributes_fragment(adapter_instance, cached_attributes = {}) ⇒ Object
1.
-
#object_cache_key ⇒ Object
Use object’s cache_key if available, else derive a key from the object Pass the
keyoption to thecachedeclaration or override this method to customize the cache key. - #serializer_class ⇒ Object
Instance Method Details
#cache_key(adapter_instance) ⇒ Object
270 271 272 273 274 275 276 277 278 |
# File 'lib/active_model/serializer/concerns/caching.rb', line 270 def cache_key(adapter_instance) return @cache_key if defined?(@cache_key) parts = [] parts << object_cache_key parts << adapter_instance.cache_key parts << serializer_class._cache_digest unless serializer_class._skip_digest? @cache_key = (parts) end |
#expand_cache_key(parts) ⇒ Object
280 281 282 |
# File 'lib/active_model/serializer/concerns/caching.rb', line 280 def (parts) ActiveSupport::Cache.(parts) end |
#fetch(adapter_instance, cache_options = serializer_class._cache_options, key = nil) ⇒ Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/active_model/serializer/concerns/caching.rb', line 232 def fetch(adapter_instance, = serializer_class., key = nil) if serializer_class.cache_store key ||= cache_key(adapter_instance) serializer_class.cache_store.fetch(key, ) do yield end else yield end end |
#fetch_attributes(fields, cached_attributes, adapter_instance) ⇒ Object
INSTANCE METHODS
223 224 225 226 227 228 229 230 |
# File 'lib/active_model/serializer/concerns/caching.rb', line 223 def fetch_attributes(fields, cached_attributes, adapter_instance) key = cache_key(adapter_instance) cached_attributes.fetch(key) do fetch(adapter_instance, serializer_class., key) do attributes(fields, true) end end end |
#fetch_attributes_fragment(adapter_instance, cached_attributes = {}) ⇒ Object
-
Determine cached fields from serializer class options
-
Get non_cached_fields and fetch cache_fields
-
Merge the two hashes using adapter_instance#fragment_cache
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/active_model/serializer/concerns/caching.rb', line 246 def fetch_attributes_fragment(adapter_instance, cached_attributes = {}) serializer_class. ||= {} serializer_class.[:key] = serializer_class._cache_key if serializer_class._cache_key fields = serializer_class.fragmented_attributes non_cached_fields = fields[:non_cached].dup non_cached_hash = attributes(non_cached_fields, true) include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys) non_cached_hash.merge! associations_hash({}, { include_directive: include_directive }, adapter_instance) cached_fields = fields[:cached].dup key = cache_key(adapter_instance) cached_hash = cached_attributes.fetch(key) do fetch(adapter_instance, serializer_class., key) do hash = attributes(cached_fields, true) include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys) hash.merge! associations_hash({}, { include_directive: include_directive }, adapter_instance) end end # Merge both results adapter_instance.fragment_cache(cached_hash, non_cached_hash) end |
#object_cache_key ⇒ Object
Use object’s cache_key if available, else derive a key from the object Pass the key option to the cache declaration or override this method to customize the cache key
286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/active_model/serializer/concerns/caching.rb', line 286 def object_cache_key if object.respond_to?(:cache_key_with_version) object.cache_key_with_version elsif object.respond_to?(:cache_key) object.cache_key elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class.[:key])) object_time_safe = object.updated_at object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime) "#{serializer_cache_key}/#{object.id}-#{object_time_safe}" else fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{serializer_class}.cache'" end end |
#serializer_class ⇒ Object
300 301 302 |
# File 'lib/active_model/serializer/concerns/caching.rb', line 300 def serializer_class @serializer_class ||= self.class end |