class Garner::Cache::Identity

Attributes

bindings[RW]
key_hash[RW]
options_hash[RW]
ruby_context[RW]

Public Class Methods

new(ruby_context = nil) click to toggle source
# File lib/garner/cache/identity.rb, line 7
def initialize(ruby_context = nil)
  @ruby_context = ruby_context

  @bindings = []
  @key_hash = {}

  # Set up options hash with defaults
  @options_hash = Garner.config.global_cache_options.dup || {}
  @options_hash[:expires_in] ||= Garner.config.expires_in
end

Public Instance Methods

bind(object, &block) click to toggle source

Bind this cache identity to a (bindable) object.

@param object [Object] An object; should support configured binding strategy.

# File lib/garner/cache/identity.rb, line 35
def bind(object, &block)
  @bindings << object
  block_given? ? fetch(&block) : self
end
fetch() { || ... } click to toggle source
# File lib/garner/cache/identity.rb, line 18
def fetch(&block)
  if @nocache
    yield
  else
    Garner::Cache.fetch(@bindings, @key_hash, @options_hash, &block)
  end
end
key(hash, &block) click to toggle source

Merge the given hash into the cache identity’s key hash.

@param hash [Hash] A hash to merge on top of the current key hash.

# File lib/garner/cache/identity.rb, line 43
def key(hash, &block)
  @key_hash.merge!(hash)
  block_given? ? fetch(&block) : self
end
nocache() click to toggle source

Disable caching for this identity.

# File lib/garner/cache/identity.rb, line 27
def nocache
  @nocache = true
  block_given? ? fetch(&block) : self
end
options(hash, &block) click to toggle source

Merge the given hash into the cache identity’s cache options. Any cache_options supported by Garner.config.cache may be passed.

@param hash [Hash] Options to pass to Garner.config.cache.

# File lib/garner/cache/identity.rb, line 52
def options(hash, &block)
  @options_hash.merge!(hash)
  block_given? ? fetch(&block) : self
end