Module: HalApi::Representer::Caches
- Extended by:
- ActiveSupport::Concern
- Included in:
- HalApi::Representer
- Defined in:
- lib/hal_api/representer/caches.rb
Overview
expects underlying model to have filename, class, and id attributes
Defined Under Namespace
Classes: SerializedJson
Instance Method Summary collapse
- #cache ⇒ Object
-
#cache_key(obj, options) ⇒ Object
This isn’t working, comment out this optimization def create_representation_with(doc, options, format) cache.fetch(cache_key(represented, options), cache_options) do response = super(doc, options, format) response = HalApi::Representer::Caches::SerializedJson.new(JSON.dump(response)) if (options == :json) response end end.
- #cache_key_class_name ⇒ Object
- #cache_options ⇒ Object
-
#to_json(options = {}) ⇒ Object
Pass in an option for the format this is going ‘to_` used in caching the final string format of the obj rather than the intermediary `Hash`, a modest accelerant.
Instance Method Details
#cache ⇒ Object
34 35 36 |
# File 'lib/hal_api/representer/caches.rb', line 34 def cache Rails.cache end |
#cache_key(obj, options) ⇒ Object
This isn’t working, comment out this optimization def create_representation_with(doc, options, format)
cache.fetch(cache_key(represented, ), ) do
response = super(doc, , format)
response = HalApi::Representer::Caches::SerializedJson.new(JSON.dump(response)) if ([:to_] == :json)
response
end
end
25 26 27 28 29 30 31 32 |
# File 'lib/hal_api/representer/caches.rb', line 25 def cache_key(obj, ) key_components = [cache_key_class_name] key_components << (obj.try(:is_root_resource) ? 'r' : 'l') key_components << obj key_components << .select{|k,v| Array(['_keys']).include?(k.to_s)} ActiveSupport::Cache.(key_components) end |
#cache_key_class_name ⇒ Object
38 39 40 |
# File 'lib/hal_api/representer/caches.rb', line 38 def cache_key_class_name self.class.name.underscore.gsub(/_representer$/, "") end |
#cache_options ⇒ Object
42 43 44 |
# File 'lib/hal_api/representer/caches.rb', line 42 def {compress: true, race_condition_ttl: 10, expires_in: 1.hour} end |
#to_json(options = {}) ⇒ Object
Pass in an option for the format this is going ‘to_` used in caching the final string format of the obj rather than the intermediary `Hash`, a modest accelerant
11 12 13 14 |
# File 'lib/hal_api/representer/caches.rb', line 11 def to_json(={}) [:to_] = :json super() end |