Class: Card::Cache

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

Defined Under Namespace

Classes: Persistent, Temporary

Constant Summary collapse

TEST_ENVS =
%w{test cucumber}
@@prepopulating =
TEST_ENVS.include? Rails.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.



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

def initialize opts={}
  @klass = opts[:class]
  cache_by_class[@klass] = self

  # hard cache mirrors the db
  # only difference to db: it caches virtual cards
  @hard = Persistent.new opts if opts[:store]

  # soft cache is temporary
  # lasts only one request/script execution/console session
  @soft = Temporary.new
end

Instance Attribute Details

#hardObject (readonly)

Returns the value of attribute hard.



106
107
108
# File 'lib/card/cache.rb', line 106

def hard
  @hard
end

#softObject (readonly)

Returns the value of attribute soft.



106
107
108
# File 'lib/card/cache.rb', line 106

def soft
  @soft
end

Class Method Details

.[](klass) ⇒ Object



24
25
26
27
28
29
# File 'lib/card/cache.rb', line 24

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



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

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

.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_soft
end

.reset_allObject



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

def reset_all
  reset_hard
  reset_soft

  Card::Codename.reset_cache
  Cardio.delete_tmp_files
end

.reset_globalObject

deprecated



51
52
53
# File 'lib/card/cache.rb', line 51

def reset_global # deprecated
  reset_all
end

.reset_hardObject



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

def reset_hard
  cache_by_class.each do |_klass, cache|
    cache.hard.reset if cache.hard
  end
end

.reset_softObject



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

def reset_soft
  cache_by_class.each do |_klass, cache|
    cache.soft.reset
  end
end

.restoreObject



46
47
48
49
# File 'lib/card/cache.rb', line 46

def restore
  reset_soft
  prepopulate
end

.system_prefix(klass) ⇒ Object



42
43
44
# File 'lib/card/cache.rb', line 42

def system_prefix klass
  "#{Rails.env}/#{klass}"
end

Instance Method Details

#delete(key) ⇒ Object



141
142
143
144
# File 'lib/card/cache.rb', line 141

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

#dumpObject



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

def dump
  p 'dumping temporary request cache....'
  @soft.dump
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#fetch(key, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/card/cache.rb', line 131

def fetch key, &block
  @soft.fetch(key) do
    if @hard
      @hard.fetch(key, &block)
    else
      block.call
    end
  end
end

#read(key) ⇒ Object



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

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

#resetObject



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

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

#write(key, value) ⇒ Object



126
127
128
129
# File 'lib/card/cache.rb', line 126

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