Class: Props::Cache
- Inherits:
-
Object
- Object
- Props::Cache
- Defined in:
- lib/props_template/extensions/cache.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#cache(key = nil, options = {}) ⇒ Object
Copied from jbuilder.
- #cache_fragment_for(key, options, &block) ⇒ Object
- #cache_key(key, options) ⇒ Object
- #context ⇒ Object
- #fragment_name_with_digest(key, options) ⇒ Object
-
#initialize(context) ⇒ Cache
constructor
A new instance of Cache.
- #instrument(name, **options) ⇒ Object
- #multi_fetch(keys, options = {}) ⇒ Object
- #multi_fetch_and_add_results(all_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.
20 21 22 |
# File 'lib/props_template/extensions/cache.rb', line 20 def initialize(context) @context = context end |
Class Method Details
.refine_options(options, item = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/props_template/extensions/cache.rb', line 5 def self.(, item = nil) return if ![:cache] pass_opts = .clone key, rest = [*[:cache]] rest ||= {} if item && ::Proc === key key = key.call(item) end pass_opts[:cache] = [key, rest] pass_opts end |
Instance Method Details
#cache(key = nil, options = {}) ⇒ Object
Copied from jbuilder
91 92 93 94 95 96 97 98 99 |
# File 'lib/props_template/extensions/cache.rb', line 91 def cache(key=nil, ={}) if controller.perform_caching value = cache_fragment_for(key, ) do yield end else yield end end |
#cache_fragment_for(key, options, &block) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/props_template/extensions/cache.rb', line 101 def cache_fragment_for(key, , &block) key = cache_key(key, ) return [:result] if [:result] read_fragment_cache(key, ) || write_fragment_cache(key, , &block) end |
#cache_key(key, options) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/props_template/extensions/cache.rb', line 123 def cache_key(key, ) = .slice(:skip_digest, :virtual_path) key = fragment_name_with_digest(key, ) if @context.respond_to?(:combined_fragment_cache_key) key = @context.combined_fragment_cache_key(key) else key = url_for(key).split('://', 2).last if ::Hash === key end ::ActiveSupport::Cache.(key, :props) end |
#context ⇒ Object
24 25 26 |
# File 'lib/props_template/extensions/cache.rb', line 24 def context @context end |
#fragment_name_with_digest(key, options) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/props_template/extensions/cache.rb', line 136 def fragment_name_with_digest(key, ) if @context.respond_to?(:cache_fragment_name) # Current compatibility, fragment_name_with_digest is private again and cache_fragment_name # should be used instead. @context.cache_fragment_name(key, ) elsif @context.respond_to?(:fragment_name_with_digest) # Backwards compatibility for period of time when fragment_name_with_digest was made public. @context.fragment_name_with_digest(key) else key end end |
#instrument(name, **options) ⇒ Object
28 29 30 31 32 |
# File 'lib/props_template/extensions/cache.rb', line 28 def instrument(name, **) ActiveSupport::Notifications.instrument(name, ) do |payload| yield payload end end |
#multi_fetch(keys, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/props_template/extensions/cache.rb', line 34 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 = {} 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] result[k] = read_caches[ckey] end result end |
#multi_fetch_and_add_results(all_options) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/props_template/extensions/cache.rb', line 65 def multi_fetch_and_add_results() first_opts = [0] if first_opts[:cache] && controller.perform_caching keys = .map{|i| i[:cache][0]} c_opts = first_opts[:cache][1] result = multi_fetch(keys, c_opts) .map do |opts| key = opts[:cache][0] if result.key? key opts[:cache][1][:result] = result[key] opts else opts end end else end end |
#read_fragment_cache(key, options = nil) ⇒ Object
109 110 111 112 113 |
# File 'lib/props_template/extensions/cache.rb', line 109 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
115 116 117 118 119 120 121 |
# File 'lib/props_template/extensions/cache.rb', line 115 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 |