Class: Wagn::Cache

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

Constant Summary collapse

@@prepopulating =
[ 'test','cucumber' ].include? Rails.env
@@using_rails_cache =
Rails.env =~ /^cucumber|test$/
@@prefix_root =
@@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.



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

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.



85
86
87
# File 'lib/wagn/cache.rb', line 85

def klass
  @klass
end

#localObject

Returns the value of attribute local.



86
87
88
# File 'lib/wagn/cache.rb', line 86

def local
  @local
end

#prefixObject (readonly)

Returns the value of attribute prefix.



85
86
87
# File 'lib/wagn/cache.rb', line 85

def prefix
  @prefix
end

#storeObject (readonly)

Returns the value of attribute store.



85
86
87
# File 'lib/wagn/cache.rb', line 85

def store
  @store
end

Class Method Details

.[](klass) ⇒ Object



25
26
27
28
# File 'lib/wagn/cache.rb', line 25

def [] klass
  raise "nil klass" if klass.nil?
  cache_by_class[klass] ||= new :class=>klass, :store=>(@@using_rails_cache ? nil : Rails.cache)
end

.generate_cache_idObject



50
51
52
# File 'lib/wagn/cache.rb', line 50

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

.renewObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/wagn/cache.rb', line 30

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



54
55
56
57
58
59
60
# File 'lib/wagn/cache.rb', line 54

def reset_global
  cache_by_class.each do |klass, cache|
    cache.reset hard=true
  end
  Card::Codename.reset_cache
  Card.delete_tmp_files
end

.reset_localObject



62
63
64
65
66
67
68
# File 'lib/wagn/cache.rb', line 62

def reset_local
  cache_by_class.each do |cc, cache|
    if Wagn::Cache===cache
      cache.reset_local
    else warn "reset class #{cc}, #{cache.class} #{caller[0..8]*"\n"} ???" end
  end
end

.restore(klass = nil) ⇒ Object



45
46
47
48
# File 'lib/wagn/cache.rb', line 45

def restore klass=nil
  reset_local
  prepopulate
end

.system_prefix(klass) ⇒ Object



41
42
43
# File 'lib/wagn/cache.rb', line 41

def system_prefix klass
  "#{ prefix_root }/#{ klass }"
end

Instance Method Details

#delete(key) ⇒ Object



155
156
157
158
# File 'lib/wagn/cache.rb', line 155

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

#dumpObject



160
161
162
163
164
165
# File 'lib/wagn/cache.rb', line 160

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

#fetch(key, &block) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/wagn/cache.rb', line 141

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



151
152
153
# File 'lib/wagn/cache.rb', line 151

def fetch_local key
  read_local key or write_local key, yield
end

#read(key) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/wagn/cache.rb', line 111

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



119
120
121
# File 'lib/wagn/cache.rb', line 119

def read_local key
  @local[key]
end

#reset(hard = false) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/wagn/cache.rb', line 167

def reset hard=false
  reset_local
  @cache_id = self.class.generate_cache_id
  if @store
    if hard
      @store.clear
    else
      @store.write @system_prefix + "cache_id", @cache_id
    end
  end
  @prefix = @system_prefix + @cache_id + "/"
end

#reset_localObject



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

def reset_local
  @local = {}
end

#system_prefix=(system_prefix) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wagn/cache.rb', line 98

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



132
133
134
135
# File 'lib/wagn/cache.rb', line 132

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

#write_local(key, value) ⇒ Object



137
138
139
# File 'lib/wagn/cache.rb', line 137

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

#write_variable(key, variable, value) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/wagn/cache.rb', line 123

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