Module: Webs::Cache
- Defined in:
- lib/cache/cache.rb
Instance Method Summary collapse
- #cache_block(key, options = {}, &block) ⇒ Object
- #cache_capture_data(&block) ⇒ Object
- #cache_read(key) ⇒ Object
- #cache_remove(key) ⇒ Object
-
#cache_snippet(snippet, key, options = {}, &block) ⇒ Object
caching snippets works very similiarly to caching a block except that snippets have already been read to check for existence so we don’t want to do a re-read.
- #cache_write(key, data, options = {}) ⇒ Object
Instance Method Details
#cache_block(key, options = {}, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cache/cache.rb', line 50 def cache_block(key, ={}, &block) return if key.nil? Rails.logger.debug "*********** cache_block( #{key} )" if debug return cache_capture_data( &block ) if off unless ( data=cache_read(key) ) # if self responds to capture this is called from a view which means we can cache snippets data = cache_capture_data( &block ) cache_write( key.to_s, data, ) end data rescue Exception => e Rails.logger.error "------------------------------------------------\nmemcache_error: #{e.}, #{e.backtrace.join("\n")}" data end |
#cache_capture_data(&block) ⇒ Object
46 47 48 |
# File 'lib/cache/cache.rb', line 46 def cache_capture_data( &block ) self.respond_to?('capture') ? capture(&block) : yield end |
#cache_read(key) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/cache/cache.rb', line 17 def cache_read( key ) return nil if off data = Webs.cache.respond_to?( 'read' ) ? Webs.cache.read(key.to_s) : Webs.cache[ key.to_s ] Rails.logger.debug "****#{data ? 'HIT*' : 'MISS'}*** cache_read( #{key} )" if debug data rescue Exception => e Rails.logger.error "------------------------------------------------\nmemcache_error: #{e.}, #{e.backtrace.join("\n")}" nil end |
#cache_remove(key) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/cache/cache.rb', line 8 def cache_remove(key) Rails.logger.debug "*********** cache_remove( #{key} )" if debug return if key.nil? || off Webs.cache.delete(key.to_s) rescue Exception => e Rails.logger.error "------------------------------------------------\nmemcache_error: #{e.}" false end |
#cache_snippet(snippet, key, options = {}, &block) ⇒ Object
caching snippets works very similiarly to caching a block except that snippets have already been read to check for existence so we don’t want to do a re-read. So generally in the controller you will do a cache_read on the key, setting a snippet @var, not do normal processing, then in view do a cache_snippet which checks to see if snippet param is set if not it will cache_block the snippet view otherwide just render the passed snippet
72 73 74 75 76 77 78 79 |
# File 'lib/cache/cache.rb', line 72 def cache_snippet(snippet, key, ={}, &block) return snippet.html_safe if !snippet.blank? if .delete(:ignore_cache) cache_capture_data( &block ) else cache_block( key, , &block ) end end |
#cache_write(key, data, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cache/cache.rb', line 27 def cache_write( key, data, ={} ) s_data = Marshal.dump(data) block_size = s_data.size #TODO: Compression? if block_size < 1.megabyte Rails.logger.debug "*********** cache_write[#{block_size}]( #{key}, #{data && data.inspect.length > 100 ? data.inspect[0..100] : data} )" if debug if Webs.cache.respond_to?( 'write' ) Webs.cache.write(key.to_s, data) else Webs.cache[ key.to_s ] = data end else Rails.logger.debug "*********** cache_write( #{key} ) not cached since exceeds 1M @ #{block_size} )" if debug end rescue Exception => e Rails.logger.error "------------------------------------------------\nmemcache_error: #{e.}, #{e.backtrace.join("\n")}" nil end |