Class: Garner::Cache::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/garner/cache/identity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_context = nil) ⇒ Identity

Returns a new instance of Identity.



7
8
9
10
11
12
13
14
15
16
# 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 || {}
  @options_hash.merge!(expires_in: Garner.config.expires_in)
end

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings.



5
6
7
# File 'lib/garner/cache/identity.rb', line 5

def bindings
  @bindings
end

#key_hashObject

Returns the value of attribute key_hash.



5
6
7
# File 'lib/garner/cache/identity.rb', line 5

def key_hash
  @key_hash
end

#options_hashObject

Returns the value of attribute options_hash.



5
6
7
# File 'lib/garner/cache/identity.rb', line 5

def options_hash
  @options_hash
end

#ruby_contextObject

Returns the value of attribute ruby_context.



4
5
6
# File 'lib/garner/cache/identity.rb', line 4

def ruby_context
  @ruby_context
end

Instance Method Details

#bind(object, &block) ⇒ Object

Bind this cache identity to a (bindable) object.

Parameters:

  • object (Object)

    An object; should support configured binding strategy.



35
36
37
38
# File 'lib/garner/cache/identity.rb', line 35

def bind(object, &block)
  @bindings << object
  block_given? ? fetch(&block) : self
end

#fetch(&block) ⇒ Object



18
19
20
21
22
23
24
# 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) ⇒ Object

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

Parameters:

  • hash (Hash)

    A hash to merge on top of the current key hash.



43
44
45
46
# File 'lib/garner/cache/identity.rb', line 43

def key(hash, &block)
  @key_hash.merge!(hash)
  block_given? ? fetch(&block) : self
end

#nocacheObject

Disable caching for this identity.



27
28
29
30
# File 'lib/garner/cache/identity.rb', line 27

def nocache
  @nocache = true
  block_given? ? fetch(&block) : self
end

#options(hash, &block) ⇒ Object

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

Parameters:

  • hash (Hash)

    Options to pass to Garner.config.cache.



52
53
54
55
# File 'lib/garner/cache/identity.rb', line 52

def options(hash, &block)
  @options_hash.merge!(hash)
  block_given? ? fetch(&block) : self
end