Module: Seabright::ViewCaching::ClassMethods
- Defined in:
- lib/redis_object/ext/view_caching.rb
Instance Method Summary collapse
- #cache_invalidation_hooks ⇒ Object
- #cache_named_views! ⇒ Object
- #cache_view(name, opts = true) ⇒ Object (also: #cache_named_view)
- #cached_views ⇒ Object
- #convert_to_collection_names(names) ⇒ Object
- #downstream_invalidations ⇒ Object
- #intercept_views_for_caching! ⇒ Object
- #invalidate_caches_from_upstream_updates! ⇒ Object
-
#invalidate_downstream(*args) ⇒ Object
def invalidate_everything_upstream! @invalidate_everything_upstream = true end.
- #invalidate_upstream(*args) ⇒ Object
- #on_cache_invalidation(&block) ⇒ Object
- #set_up_invalidation! ⇒ Object
- #upstream_invalidations ⇒ Object
-
#view_should_be_cached?(name) ⇒ Boolean
def invalidate_everything_downstream! @invalidate_everything_downstream = true end.
Instance Method Details
#cache_invalidation_hooks ⇒ Object
223 224 225 |
# File 'lib/redis_object/ext/view_caching.rb', line 223 def cache_invalidation_hooks @cache_invalidation_hooks ||= [] end |
#cache_named_views! ⇒ Object
280 281 282 283 284 |
# File 'lib/redis_object/ext/view_caching.rb', line 280 def cache_named_views! named_views.each do |name,view| cache_view name end end |
#cache_view(name, opts = true) ⇒ Object Also known as: cache_named_view
35 36 37 |
# File 'lib/redis_object/ext/view_caching.rb', line 35 def cache_view(name,opts=true) cached_views[name.to_sym] = opts end |
#cached_views ⇒ Object
276 277 278 |
# File 'lib/redis_object/ext/view_caching.rb', line 276 def cached_views @cached_view ||= {} end |
#convert_to_collection_names(names) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/redis_object/ext/view_caching.rb', line 247 def convert_to_collection_names(names) names.map do |name| case name when RedisObject name.collection_name when String, Symbol name.to_s.pluralize == name.to_s ? name.to_sym : name.to_s.pluralize.to_sym else name end end end |
#downstream_invalidations ⇒ Object
260 261 262 |
# File 'lib/redis_object/ext/view_caching.rb', line 260 def downstream_invalidations @downstream_invalidations ||= [] end |
#intercept_views_for_caching! ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/redis_object/ext/view_caching.rb', line 40 def intercept_views_for_caching! return if @cached_views_intercepted self.class_eval do alias_method :uncached_view_as_hash, :view_as_hash unless method_defined?(:uncached_view_as_hash) def view_as_hash(name) return uncached_view_as_hash(name) unless self.class.view_should_be_cached?(name) if v = view_from_cache(name) puts " Got view from cache: #{name}" if DEBUG Yajl::Parser.parse(v) else puts " View cache miss: #{name}" if DEBUG cache_view_content(name)[0] end end alias_method :uncached_view_as_json, :view_as_json unless method_defined?(:uncached_view_as_json) def view_as_json(name) return uncached_view_as_json(name) unless self.class.view_should_be_cached?(name) if v = view_from_cache(name) puts " Got view from cache: #{name}" if DEBUG v else puts " View cache miss: #{name}" if DEBUG cache_view_content(name)[1] end end def cache_view_content(name,content=nil) content ||= uncached_view_as_hash(name) json = Yajl::Encoder.encode(content) store.hset(cached_view_key,name,json) [content,json] end def view_from_cache(name) if v = store.hget(cached_view_key,name) v else nil end end def view_is_cached?(name) store.hexists(cached_view_key, name) end def cached_view_key "#{hkey}::ViewCache" end def regenerate_cached_views(*names) names.each do |name| cache_view_content name end end def regenerate_cached_views! regenerate_cached_views(*self.class.cached_views.map {|name,opts| name }) end end @cached_views_intercepted = true end |
#invalidate_caches_from_upstream_updates! ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/redis_object/ext/view_caching.rb', line 209 def invalidate_caches_from_upstream_updates! self.class_eval do def invalidated_by(obj,chain) invalidate_cached_views! end end end |
#invalidate_downstream(*args) ⇒ Object
def invalidate_everything_upstream! @invalidate_everything_upstream = true end
def invalidate_everything_upstream? @invalidate_everything_upstream end
243 244 245 |
# File 'lib/redis_object/ext/view_caching.rb', line 243 def invalidate_downstream(*args) @downstream_invalidations = (@downstream_invalidations || []) + convert_to_collection_names(args) end |
#invalidate_upstream(*args) ⇒ Object
227 228 229 |
# File 'lib/redis_object/ext/view_caching.rb', line 227 def invalidate_upstream(*args) @upstream_invalidations = (@upstream_invalidations || []) + args end |
#on_cache_invalidation(&block) ⇒ Object
219 220 221 |
# File 'lib/redis_object/ext/view_caching.rb', line 219 def on_cache_invalidation(&block) cache_invalidation_hooks << block end |
#set_up_invalidation! ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/redis_object/ext/view_caching.rb', line 105 def set_up_invalidation! return if @invalidation_set_up self.class_eval do def invalidate_cached_views(*names) puts "Invalidating cached views: #{names.join(", ")}" if Debug.verbose? run_script(:CachedViewInvalidator, [cached_view_key], names, CachedViewInvalidator) self.class.cache_invalidation_hooks do |hook| hook.call(names) end end alias_method :invalidate_cached_view, :invalidate_cached_views def invalidate_cached_views! invalidate_cached_views(*self.class.cached_views.map {|name,opts| name }) end def invalidations(stream) self.class.send("#{stream}stream_invalidations".to_sym) end def invalidate_downstream! return unless invalidations(:down).size > 0 puts "Invalidating downstream: #{invalidations(:down).inspect}" if Debug.verbose? invalidations(:down).each do |col| if has_collection?(col) and (colctn = get_collection(col)) colctn.each do |obj| obj.invalidated_by_other(self,invalidation_chain + [self.hkey]) end end end end def invalidate_upstream! return unless invalidations(:up).size > 0 puts "Invalidating upstream: #{invalidations(:up).inspect}" if Debug.verbose? backreferences.each do |obj| if invalidations(:up).include?(obj.class) and obj.respond_to?(:invalidated_by_other) obj.invalidated_by_other(self,invalidation_chain + [self.hkey]) end end end def invalidated_by_update!(*args) Thread.new do invalidate_cached_views! invalidate_up_and_down! end end def invalidated_by_reference!(*args) invalidated_by_update! end def invalidation_chain @invalidation_chain ||= [] end def invalidate_up_and_down! unless invalidation_chain.include?(self) invalidate_downstream! invalidate_upstream! end end def invalidated_by_other(obj,chain) return if chain.include?(self.hkey) puts "#{self.class.name}:#{self.id}'s view caches were invalidated by upstream object: #{obj.class.name}:#{obj.id} (chain:#{chain.inspect})" if Debug.verbose? @invalidation_chain = chain [:invalidated_by,"invalidated_by_#{obj.class.name.underscore}".to_sym].each do |meth_sym| if respond_to?(meth_sym) send(meth_sym,obj,chain) end end invalidate_up_and_down! end def invalidated_by(obj,chain) invalidate_cached_views! end def invalidated_set(cmd,k,v) ret = send("uninvalidated_#{cmd}".to_sym,k,v) invalidated_by_update! ret end alias_method :uninvalidated_set, :set unless method_defined?(:uninvalidated_set) def set(k,v) invalidated_set(:set,k,v) end alias_method :uninvalidated_setnx, :setnx unless method_defined?(:uninvalidated_setnx) def setnx(k,v) invalidated_set(:setnx,k,v) end # trigger_on_update :invalidated_by_update! trigger_on_reference :invalidated_by_reference! end @invalidation_set_up = true end |
#upstream_invalidations ⇒ Object
231 232 233 |
# File 'lib/redis_object/ext/view_caching.rb', line 231 def upstream_invalidations @upstream_invalidations ||= [] end |
#view_should_be_cached?(name) ⇒ Boolean
def invalidate_everything_downstream! @invalidate_everything_downstream = true end
def invalidate_everything_downstream? @invalidate_everything_downstream end
272 273 274 |
# File 'lib/redis_object/ext/view_caching.rb', line 272 def view_should_be_cached?(name) !!cached_views[name.to_sym] end |