Method: Itrigga::Cache::ClassMethods#with_cache

Defined in:
lib/itrigga/cache/cache.rb

#with_cache(opts = {}, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/itrigga/cache/cache.rb', line 75

def with_cache(opts = {}, &block)
  require_param(opts, :key)
  
  # if no cache then just return whatever the block gives us
  unless caching_enabled?(opts)
    cache_log "Cache not enabled!", opts
    return block.call
  end
  
  # see if the key is already in cache
  value = get_from_cache(opts[:key], opts)
  
  # if no match then call the block and save result in cache
  unless value
    cache_log "Key '#{opts[:key]}' missing! Calling block and setting in cache", opts
    value = block.call
    set_to_cache(opts.delete(:key), value, opts) rescue value # incase memcache crashes or whateversolr1-internal-itrigga.dyndns-ip.com solr1-internal-itrigga.dyndns-ip.com            
  else
    cache_log "Key '#{opts[:key]}' found in cache!", opts
  end
    
  value
end