Class: Card::Cache
Constant Summary collapse
- TEST_ENVS =
%w{test cucumber}
- @@prepopulating =
TEST_ENVS.include? Rails.env
- @@using_rails_cache =
TEST_ENVS.include? Rails.env
- @@cache_by_class =
{}
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#local ⇒ Object
Returns the value of attribute local.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Class Method Summary collapse
- .[](klass) ⇒ Object
- .generate_cache_id ⇒ Object
- .prefix_root ⇒ Object
- .renew ⇒ Object
- .reset_global ⇒ Object
- .reset_local ⇒ Object
- .restore(klass = nil) ⇒ Object
- .system_prefix(klass) ⇒ Object
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #dump ⇒ Object
- #fetch(key, &block) ⇒ Object
- #fetch_local(key) ⇒ Object
-
#initialize(opts = {}) ⇒ Cache
constructor
A new instance of Cache.
- #read(key) ⇒ Object
- #read_local(key) ⇒ Object
- #reset(hard = false) ⇒ Object
- #reset_local ⇒ Object
- #system_prefix=(system_prefix) ⇒ Object
- #write(key, value) ⇒ Object
- #write_local(key, value) ⇒ Object
- #write_variable(key, variable, value) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Cache
Returns a new instance of Cache.
98 99 100 101 102 103 104 105 |
# File 'lib/card/cache.rb', line 98 def initialize opts={} #warn "new cache #{opts.inspect}" @klass = opts[:class] @store = opts[:store] @local = Hash.new self.system_prefix = opts[:prefix] || self.class.system_prefix(opts[:class]) cache_by_class[klass] = self end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
95 96 97 |
# File 'lib/card/cache.rb', line 95 def klass @klass end |
#local ⇒ Object
Returns the value of attribute local.
96 97 98 |
# File 'lib/card/cache.rb', line 96 def local @local end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
95 96 97 |
# File 'lib/card/cache.rb', line 95 def prefix @prefix end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
95 96 97 |
# File 'lib/card/cache.rb', line 95 def store @store end |
Class Method Details
.[](klass) ⇒ Object
26 27 28 29 |
# File 'lib/card/cache.rb', line 26 def [] klass raise "nil klass" if klass.nil? cache_by_class[klass] ||= new :class=>klass, :store=>(@@using_rails_cache ? nil : Cardio.cache) end |
.generate_cache_id ⇒ Object
56 57 58 |
# File 'lib/card/cache.rb', line 56 def generate_cache_id ((Time.now.to_f * 100).to_i).to_s + ('a'..'z').to_a[rand(26)] + ('a'..'z').to_a[rand(26)] end |
.prefix_root ⇒ Object
42 43 44 45 |
# File 'lib/card/cache.rb', line 42 def prefix_root @@prefix_root ||= ( cfg = Cardio.config and cfg = cfg.database_configuration and cfg[Rails.env]['database'] ) end |
.renew ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/card/cache.rb', line 31 def renew cache_by_class.keys do |klass| if klass.cache cache_by_class[klass].system_prefix = system_prefix(klass) else raise "renewing nil cache: #{klass}" end end reset_local end |
.reset_global ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/card/cache.rb', line 60 def reset_global cache_by_class.each do |klass, cache| cache.reset hard=true end Card::Codename.reset_cache Cardio.delete_tmp_files end |
.reset_local ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/card/cache.rb', line 68 def reset_local cache_by_class.each do |cc, cache| if Card::Cache===cache cache.reset_local else warn "reset class #{cc}, #{cache.class} #{caller[0..8]*"\n"} ???" end end end |
.restore(klass = nil) ⇒ Object
51 52 53 54 |
# File 'lib/card/cache.rb', line 51 def restore klass=nil reset_local prepopulate end |
.system_prefix(klass) ⇒ Object
47 48 49 |
# File 'lib/card/cache.rb', line 47 def system_prefix klass "#{ prefix_root }/#{ klass }" end |
Instance Method Details
#delete(key) ⇒ Object
165 166 167 168 |
# File 'lib/card/cache.rb', line 165 def delete key @store.delete(@prefix + key) if @store @local.delete key end |
#dump ⇒ Object
170 171 172 173 174 175 |
# File 'lib/card/cache.rb', line 170 def dump p "dumping local...." @local.each do |k, v| p "#{k} --> #{v.inspect[0..30]}" end end |
#fetch(key, &block) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/card/cache.rb', line 151 def fetch key, &block fetch_local key do if @store @store.fetch(@prefix + key, &block) else block.call end end end |
#fetch_local(key) ⇒ Object
161 162 163 |
# File 'lib/card/cache.rb', line 161 def fetch_local key read_local key or write_local key, yield end |
#read(key) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/card/cache.rb', line 121 def read key if @local.has_key? key read_local key elsif @store write_local key, @store.read(@prefix + key) end end |
#read_local(key) ⇒ Object
129 130 131 |
# File 'lib/card/cache.rb', line 129 def read_local key @local[key] end |
#reset(hard = false) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/card/cache.rb', line 177 def reset hard=false reset_local @cache_id = self.class.generate_cache_id if @store if hard begin @store.clear rescue => e Rails.logger.debug "Problem clearing cache: #{e.}" end else @store.write @system_prefix + "cache_id", @cache_id end end @prefix = @system_prefix + @cache_id + "/" end |
#reset_local ⇒ Object
194 195 196 |
# File 'lib/card/cache.rb', line 194 def reset_local @local = {} end |
#system_prefix=(system_prefix) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/card/cache.rb', line 108 def system_prefix= system_prefix @system_prefix = system_prefix if @store.nil? @prefix = system_prefix + self.class.generate_cache_id + "/" else @system_prefix += '/' unless @system_prefix[-1] == '/' @cache_id = @store.fetch(@system_prefix + "cache_id") do self.class.generate_cache_id end @prefix = @system_prefix + @cache_id + "/" end end |
#write(key, value) ⇒ Object
142 143 144 145 |
# File 'lib/card/cache.rb', line 142 def write key, value @store.write(@prefix + key, value) if @store write_local key, value end |
#write_local(key, value) ⇒ Object
147 148 149 |
# File 'lib/card/cache.rb', line 147 def write_local key, value @local[key] = value end |
#write_variable(key, variable, value) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/card/cache.rb', line 133 def write_variable key, variable, value key = @prefix + key if @store and object = @store.read(key) object.instance_variable_set "@#{ variable }", value @store.write key, object end value end |