Class: Card::Cache

Inherits:
Object
  • Object
show all
Extended by:
Prepopulate
Defined in:
lib/card/cache.rb,
lib/card/cache/temporary.rb,
lib/card/cache/persistent.rb,
lib/card/cache/prepopulate.rb

Overview

The Cache class manages and integrates Temporary and Persistent caching. The Temporary cache is typically process- and request- specific and is often "ahead" of the database; the Persistent cache is typically shared across processes and tends to stay true to the database.

Any ruby Class can declare and/or retrieve its own cache as follows:

Card::Cache[MyClass]

Typically speaking, mod developers do not need to use the Cache classes directly, because caching is automatically handled by Card#fetch

Defined Under Namespace

Modules: Prepopulate Classes: Persistent, Temporary

Constant Summary collapse

@@cache_by_class =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Prepopulate

restore

Constructor Details

#initialize(opts = {}) ⇒ Cache

Cache#new initializes a Temporary/soft cache, and -- if a :store opt is provided -- a Persistent/hard cache

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :store (Rails::Cache)
  • :class (Constant)


129
130
131
132
133
134
# File 'lib/card/cache.rb', line 129

def initialize opts={}
  @klass = opts[:class]
  cache_by_class[@klass] = self
  @hard = Persistent.new opts if opts[:store]
  @soft = Temporary.new
end

Instance Attribute Details

#hardObject (readonly)

Returns the value of attribute hard.



122
123
124
# File 'lib/card/cache.rb', line 122

def hard
  @hard
end

#softObject (readonly)

Returns the value of attribute soft.



122
123
124
# File 'lib/card/cache.rb', line 122

def soft
  @soft
end

Class Method Details

.[](klass) ⇒ {Card::Cache}

create a new cache for the ruby class provided

Parameters:

  • klass (Class)

Returns:



32
33
34
35
36
# File 'lib/card/cache.rb', line 32

def [] klass
  raise "nil klass" if klass.nil?
  cache_type = persistent_cache || nil
  cache_by_class[klass] ||= new class: klass, store: cache_type
end

.obj_to_key(obj) ⇒ String

generate a cache key from an object

Parameters:

  • obj (Object)

Returns:

  • (String)


110
111
112
113
114
115
116
117
118
119
# File 'lib/card/cache.rb', line 110

def obj_to_key obj
  case obj
  when Hash
    obj.sort.map { |key, value| "#{key}=>(#{obj_to_key(value)})" } * ","
  when Array
    obj.map { |value| obj_to_key(value) }
  else
    obj.to_s
  end
end

.persistent_cacheObject



38
39
40
41
42
43
44
45
46
# File 'lib/card/cache.rb', line 38

def persistent_cache
  return @persistent_cache if !@persistent_cache.nil?
  @persistent_cache =
    case
    when ENV["NO_RAILS_CACHE"]          then false
    when Cardio.config.persistent_cache then Cardio.cache
    else                                     false
    end
end

.renewObject

clear the temporary caches and ensure we're using the latest stamp on the persistent caches.



50
51
52
53
54
55
56
# File 'lib/card/cache.rb', line 50

def renew
  renew_persistent
  cache_by_class.each_value do |cache|
    cache.soft.reset
    cache.hard.renew if cache.hard
  end
end

.renew_persistentObject



58
59
60
# File 'lib/card/cache.rb', line 58

def renew_persistent
  Card::Cache::Persistent.renew if persistent_cache
end

.resetObject

reset standard cached for all classes



63
64
65
66
# File 'lib/card/cache.rb', line 63

def reset
  reset_hard
  reset_soft
end

.reset_allObject

reset all caches for all classes



69
70
71
72
73
# File 'lib/card/cache.rb', line 69

def reset_all
  reset_hard
  reset_soft
  reset_other
end

.reset_globalObject

completely wipe out all caches, often including the Persistent cache of other decks using the same mechanism. Generally prefer reset_all

See Also:



79
80
81
82
83
84
85
# File 'lib/card/cache.rb', line 79

def reset_global
  cache_by_class.each_value do |cache|
    cache.soft.reset
    cache.hard.annihilate if cache.hard
  end
  reset_other
end

.reset_hardObject

reset the Persistent cache for all classes



88
89
90
91
92
93
# File 'lib/card/cache.rb', line 88

def reset_hard
  Card::Cache::Persistent.reset if persistent_cache
  cache_by_class.each_value do |cache|
    cache.hard.reset if cache.hard
  end
end

.reset_otherObject

reset Codename cache and delete tmp files (the non-standard caches)



102
103
104
105
# File 'lib/card/cache.rb', line 102

def reset_other
  Card::Codename.reset_cache
  Cardio.delete_tmp_files!
end

.reset_softObject

reset the Temporary cache for all classes



96
97
98
# File 'lib/card/cache.rb', line 96

def reset_soft
  cache_by_class.each_value { |cache| cache.soft.reset }
end

Instance Method Details

#delete(key) ⇒ Object

delete specific cache entries by key

Parameters:

  • key (String)


161
162
163
164
# File 'lib/card/cache.rb', line 161

def delete key
  @hard.delete key if @hard
  @soft.delete key
end

#exist?(key) ⇒ true/false

test for the existence of the key in either cache

Returns:

  • (true/false)


174
175
176
# File 'lib/card/cache.rb', line 174

def exist? key
  @soft.exist?(key) || (@hard && @hard.exist?(key))
end

#fetch(key, &block) ⇒ Object

read and (if not there yet) write

Parameters:

  • key (String)


153
154
155
156
157
# File 'lib/card/cache.rb', line 153

def fetch key, &block
  @soft.fetch(key) do
    @hard ? @hard.fetch(key, &block) : yield
  end
end

#read(key) ⇒ Object

read cache value (and write to soft cache if missing)

Parameters:

  • key (String)


138
139
140
141
# File 'lib/card/cache.rb', line 138

def read key
  @soft.read(key) ||
    (@hard && (ret = @hard.read(key)) && @soft.write(key, ret))
end

#resetObject

reset both caches (for a given Card::Cache instance)



167
168
169
170
# File 'lib/card/cache.rb', line 167

def reset
  @hard.reset if @hard
  @soft.reset
end

#write(key, value) ⇒ Object

write to hard (where applicable) and soft cache

Parameters:

  • key (String)
  • value


146
147
148
149
# File 'lib/card/cache.rb', line 146

def write key, value
  @hard.write key, value if @hard
  @soft.write key, value
end