Module: CacheUtils::Controller
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/cache_utils/controller.rb
Instance Method Summary collapse
- #cache_bundle(*objects) ⇒ Object
- #cache_if(condition, name = {}, options = nil, &block) ⇒ Object
- #cache_key_for(object) ⇒ Object
- #render_with_cache(object, condition, name = {}, options = nil) ⇒ Object
Instance Method Details
#cache_bundle(*objects) ⇒ Object
5 6 7 |
# File 'lib/cache_utils/controller.rb', line 5 def cache_bundle(*objects) objects.map { |o| cache_key_for(o) }.join(':') end |
#cache_if(condition, name = {}, options = nil, &block) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/cache_utils/controller.rb', line 19 def cache_if(condition, name = {}, = nil, &block) if condition Rails.cache.fetch(name, , &block) else yield end end |
#cache_key_for(object) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/cache_utils/controller.rb', line 9 def cache_key_for(object) if object.respond_to?(:cache_key) object.cache_key elsif object.is_a?(Hash) serialize_hash(object) else object.to_s end end |
#render_with_cache(object, condition, name = {}, options = nil) ⇒ Object
27 28 29 30 31 |
# File 'lib/cache_utils/controller.rb', line 27 def render_with_cache(object, condition, name = {}, = nil) render jsonapi: cache_if(condition, name, ) { ActiveModelSerializers::SerializableResource.new(object).to_json }, raw: true end |