Class: Props::Cache
- Inherits:
-
Object
- Object
- Props::Cache
- Defined in:
- lib/props_template/extensions/cache.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Class Method Summary collapse
Instance Method Summary collapse
-
#cache(key = nil, options = {}) ⇒ Object
The below was copied from the wonderful jbuilder library Its also MIT licensed, so no issues there.
- #cache_fragment_for(key, options, &block) ⇒ Object
- #cache_key(key, options) ⇒ Object
-
#initialize(context) ⇒ Cache
constructor
A new instance of Cache.
- #load_cache(keys, options = {}) ⇒ Object
- #multi_fetch(keys, options = {}) ⇒ Object
- #read_fragment_cache(key, options = nil) ⇒ Object
- #write_fragment_cache(key, options = nil) ⇒ Object
Constructor Details
#initialize(context) ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 |
# File 'lib/props_template/extensions/cache.rb', line 7 def initialize(context) @context = context @results = {} end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
23 24 25 |
# File 'lib/props_template/extensions/cache.rb', line 23 def context @context end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
5 6 7 |
# File 'lib/props_template/extensions/cache.rb', line 5 def results @results end |
Class Method Details
.normalize_options(options, item = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/props_template/extensions/cache.rb', line 12 def self.(, item = nil) key, rest = [*] rest ||= {} if item && ::Proc === key key = key.call(item) end [key, rest] end |
Instance Method Details
#cache(key = nil, options = {}) ⇒ Object
The below was copied from the wonderful jbuilder library Its also MIT licensed, so no issues there. Thanks to the jbuilder authors!
66 67 68 69 70 71 72 73 74 |
# File 'lib/props_template/extensions/cache.rb', line 66 def cache(key = nil, = {}) if controller.perform_caching cache_fragment_for(key, ) do yield end else yield end end |
#cache_fragment_for(key, options, &block) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/props_template/extensions/cache.rb', line 76 def cache_fragment_for(key, , &block) return results[key] if results[key] key = cache_key(key, ) read_fragment_cache(key, ) || write_fragment_cache(key, , &block) end |
#cache_key(key, options) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/props_template/extensions/cache.rb', line 97 def cache_key(key, ) = .slice(:skip_digest, :virtual_path) key = @context.cache_fragment_name(key, **) if @context.respond_to?(:combined_fragment_cache_key) key = @context.combined_fragment_cache_key(key) elsif ::Hash === key key = url_for(key).split("://", 2).last end ::ActiveSupport::Cache.(key, :props) end |
#load_cache(keys, options = {}) ⇒ Object
59 60 61 |
# File 'lib/props_template/extensions/cache.rb', line 59 def load_cache(keys, = {}) @results = results.merge multi_fetch(keys, ) end |
#multi_fetch(keys, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/props_template/extensions/cache.rb', line 25 def multi_fetch(keys, = {}) result = {} key_to_ckey = {} ckeys = [] keys.each do |k| ckey = cache_key(k, ) ckeys.push(ckey) key_to_ckey[k] = ckey end payload = { controller_name: controller.controller_name, action_name: controller.action_name } read_caches = {} ActiveSupport::Notifications.instrument("read_multi_fragments.action_view", payload) do |payload| read_caches = ::Rails.cache.read_multi(*ckeys, ) payload[:read_caches] = read_caches end keys.each do |k| ckey = key_to_ckey[k] if read_caches[ckey] result[k] = read_caches[ckey] end end result end |
#read_fragment_cache(key, options = nil) ⇒ Object
83 84 85 86 87 |
# File 'lib/props_template/extensions/cache.rb', line 83 def read_fragment_cache(key, = nil) controller.instrument_fragment_cache :read_fragment, key do ::Rails.cache.read(key, ) end end |
#write_fragment_cache(key, options = nil) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/props_template/extensions/cache.rb', line 89 def write_fragment_cache(key, = nil) controller.instrument_fragment_cache :write_fragment, key do yield.tap do |value| ::Rails.cache.write(key, value, ) end end end |