lib/cache/cacheable.rb
# reads from cache if possible, otherwise executes the passed block and caches the result.
# if the passed block throws an exception, it will be logged, and the result will be cached for a much shorter time.
# WARNING: Do not use "return foo" inside the passed block or you will short-circuit the flow
# and nothing will be cached.
reads from cache if possible, otherwise executes the passed block and caches the result.
if the passed block throws an exception, it will be logged, and the result will be cached for a much shorter time.
WARNING: Do not use "return foo" inside the passed block or you will short-circuit the flow
and nothing will be cached.
reads from cache if possible otherwise executes the passed block and caches the result . if the passed block throws an exception it will be logged and the result will be cached for a much shorter time . WARNING : Do not use " return foo " inside the passed block or you will short - circuit the flow and nothing will be cached .
def smart_fetch_from_cache(opts={}, &block)
      id = opts[:id]
      key = cache_key id
      force_write = opts[:force_write]
      expiration = self.expires_in
      Rails.logger.debug "#{self.name} cache_key will be #{key}, expiration #{expiration}"
      unless force_write
        entry = Rails.cache.read key
        if entry
          Rails.logger.debug "#{self.name} Entry is already in cache: #{key}"
          return (entry == NilClass) ? nil : entry
        end
      end
      wrapped_response = handling_exceptions(key, opts, &block)
      response = wrapped_response[:response]
      if wrapped_response[:exception]
        Rails.logger.debug "#{self.name} Error occurred; writing entry to cache with short lifespan: #{key}"
        expiration = Settings.cache.expiration.failure
      else
        Rails.logger.debug "#{self.name} Writing entry to cache: #{key}"
      end
      response = process_response_before_caching(response, opts)
      cached_entry = (response.nil?) ? NilClass : response
      Rails.cache.write(key,
                        cached_entry,
                        :expires_in => expiration,
                        :force => true)
      response
    end
def smart_fetch_from_cache ( opts = { } , & block ) id = opts [ :id ] key = cache_key id force_write = opts [ :force_write ] expiration = self . expires_in Rails . logger . debug "#{self.name} cache_key will be #{key}, expiration #{expiration}" unless force_write entry = Rails . cache . read key if entry Rails . logger . debug "#{self.name} Entry is already in cache: #{key}" return ( entry == NilClass ) ? nil : entry end end wrapped_response = handling_exceptions ( key , opts , & block ) response = wrapped_response [ :response ] if wrapped_response [ :exception ] Rails . logger . debug "#{self.name} Error occurred; writing entry to cache with short lifespan: #{key}" expiration = Settings . cache . expiration . failure else Rails . logger . debug "#{self.name} Writing entry to cache: #{key}" end response = process_response_before_caching ( response , opts ) cached_entry = ( response . nil? ) ? NilClass : response Rails . cache . write ( key , cached_entry , :expires_in => expiration , :force => true ) response end
reads from cache if possible, otherwise executes the passed block and caches the result.
reads from cache if possible otherwise executes the passed block and caches the result .
opts
<not_specific>
block
<not_specific>