Class: Card::Cache
Defined Under Namespace
Classes: Persistent, Temporary
Constant Summary
collapse
- TEST_ENVS =
%w(test cucumber).freeze
- @@prepopulating =
TEST_ENVS.include? Rails.env
- @@no_rails_cache =
TEST_ENVS.include?(Rails.env) || ENV['NO_RAILS_CACHE']
- @@cache_by_class =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}) ⇒ Cache
Returns a new instance of Cache.
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/card/cache.rb', line 99
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
Returns the value of attribute hard.
97
98
99
|
# File 'lib/card/cache.rb', line 97
def hard
@hard
end
|
Returns the value of attribute soft.
97
98
99
|
# File 'lib/card/cache.rb', line 97
def soft
@soft
end
|
Class Method Details
.[](klass) ⇒ Object
13
14
15
16
17
18
|
# File 'lib/card/cache.rb', line 13
def [] klass
raise 'nil klass' if klass.nil?
cache_type = (@@no_rails_cache ? nil : Cardio.cache)
cache_by_class[klass] ||= new class: klass,
store: cache_type
end
|
.obj_to_key(obj) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/card/cache.rb', line 66
def obj_to_key obj
case obj
when Hash
obj.sort.map do |key, value|
"#{key}=>(#{obj_to_key(value)})"
end.join ','
when Array
obj.map do |value|
obj_to_key(value)
end.join ','
else
obj.to_s
end
end
|
establish clean context; clear the temporary caches and ensure we’re using the latest stamp on the persistent caches.
23
24
25
26
27
28
|
# File 'lib/card/cache.rb', line 23
def renew
cache_by_class.each do |_klass, cache|
cache.soft.reset
cache.hard.renew if cache.hard
end
end
|
.reset_all ⇒ Object
43
44
45
46
47
|
# File 'lib/card/cache.rb', line 43
def reset_all
reset_hard
reset_soft
reset_other
end
|
.reset_global ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/card/cache.rb', line 35
def reset_global
cache_by_class.each do |_klass, cache|
cache.soft.reset
cache.hard.annihilate if cache.hard
end
reset_other
end
|
.reset_hard ⇒ Object
49
50
51
52
53
|
# File 'lib/card/cache.rb', line 49
def reset_hard
cache_by_class.each do |_klass, cache|
cache.hard.reset if cache.hard
end
end
|
.reset_soft ⇒ Object
55
56
57
58
59
|
# File 'lib/card/cache.rb', line 55
def reset_soft
cache_by_class.each do |_klass, cache|
cache.soft.reset
end
end
|
30
31
32
33
|
# File 'lib/card/cache.rb', line 30
def restore
reset_soft
prepopulate
end
|
Instance Method Details
#delete(key) ⇒ Object
132
133
134
135
|
# File 'lib/card/cache.rb', line 132
def delete key
@hard.delete key if @hard
@soft.delete key
end
|
137
138
139
140
|
# File 'lib/card/cache.rb', line 137
def dump
p 'dumping temporary request cache....'
@soft.dump
end
|
#exist?(key) ⇒ Boolean
147
148
149
|
# File 'lib/card/cache.rb', line 147
def exist? key
@soft.exist?(key) || (@hard && @hard.exist?(key))
end
|
#fetch(key, &block) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/card/cache.rb', line 122
def fetch key, &block
@soft.fetch(key) do
if @hard
@hard.fetch(key, &block)
else
yield
end
end
end
|
#read(key) ⇒ Object
112
113
114
115
|
# File 'lib/card/cache.rb', line 112
def read key
@soft.read(key) ||
(@hard && (ret = @hard.read(key)) && @soft.write(key, ret))
end
|
142
143
144
145
|
# File 'lib/card/cache.rb', line 142
def reset
@hard.reset if @hard
@soft.reset
end
|
#write(key, value) ⇒ Object
117
118
119
120
|
# File 'lib/card/cache.rb', line 117
def write key, value
@hard.write key, value if @hard
@soft.write key, value
end
|