Class: Card::Cache

Inherits:
Object show all
Defined in:
lib/card/cache.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Cache

Returns a new instance of Cache.



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

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

#klassObject (readonly)

Returns the value of attribute klass.



110
111
112
# File 'lib/card/cache.rb', line 110

def klass
  @klass
end

#localObject

Returns the value of attribute local.



111
112
113
# File 'lib/card/cache.rb', line 111

def local
  @local
end

#prefixObject (readonly)

Returns the value of attribute prefix.



110
111
112
# File 'lib/card/cache.rb', line 110

def prefix
  @prefix
end

#storeObject (readonly)

Returns the value of attribute store.



110
111
112
# File 'lib/card/cache.rb', line 110

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_idObject



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

.obj_to_key(obj) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/card/cache.rb', line 76

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

.prefix_rootObject



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

.renewObject



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_globalObject



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_localObject



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



180
181
182
183
# File 'lib/card/cache.rb', line 180

def delete key
  @store.delete(@prefix + key) if @store
  @local.delete key
end

#dumpObject



185
186
187
188
189
190
# File 'lib/card/cache.rb', line 185

def dump
  p "dumping local...."
  @local.each do |k, v|
    p "#{k} --> #{v.inspect[0..30]}"
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/card/cache.rb', line 213

def exist? key
  @local.has_key?(key) || @store.exist?(key)
end

#fetch(key, &block) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/card/cache.rb', line 166

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



176
177
178
# File 'lib/card/cache.rb', line 176

def fetch_local key
  read_local key or write_local key, yield
end

#read(key) ⇒ Object



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

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



144
145
146
# File 'lib/card/cache.rb', line 144

def read_local key
  @local[key]
end

#reset(hard = false) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/card/cache.rb', line 192

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.message}"
      end
    else
      @store.write @system_prefix + "cache_id", @cache_id
    end
  end
  @prefix = @system_prefix + @cache_id + "/"
end

#reset_localObject



209
210
211
# File 'lib/card/cache.rb', line 209

def reset_local
  @local = {}
end

#system_prefix=(system_prefix) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/card/cache.rb', line 123

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



157
158
159
160
# File 'lib/card/cache.rb', line 157

def write key, value
  @store.write(@prefix + key, value) if @store
  write_local key, value
end

#write_local(key, value) ⇒ Object



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

def write_local key, value
  @local[key] = value
end

#write_variable(key, variable, value) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/card/cache.rb', line 148

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