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).freeze
@@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.



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

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.



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

def hard
  @hard
end

#softObject (readonly)

Returns the value of attribute soft.



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

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

.database_nameObject



39
40
41
42
43
# File 'lib/card/cache.rb', line 39

def database_name
  @database_name ||= (cfg = Cardio.config) &&
                     (dbcfg = cfg.database_configuration) &&
                     dbcfg[Rails.env]['database']
end

.obj_to_key(obj) ⇒ Object



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

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
# File 'lib/card/cache.rb', line 31

def renew
  cache_by_class.keys do |klass|
    raise "renewing nil cache: #{klass}" unless klass.cache
    cache_by_class[klass].system_prefix = system_prefix(klass)
  end
  reset_soft
end

.reset_allObject



58
59
60
61
62
63
64
# File 'lib/card/cache.rb', line 58

def reset_all
  reset_hard
  reset_soft

  Card::Codename.reset_cache
  Cardio.delete_tmp_files
end

.reset_globalObject

deprecated



54
55
56
# File 'lib/card/cache.rb', line 54

def reset_global # deprecated
  reset_all
end

.reset_hardObject



66
67
68
69
70
# File 'lib/card/cache.rb', line 66

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

.reset_softObject



72
73
74
75
76
# File 'lib/card/cache.rb', line 72

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

.restoreObject



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

def restore
  reset_soft
  prepopulate
end

.system_prefix(klass) ⇒ Object



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

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

Instance Method Details

#delete(key) ⇒ Object



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

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

#dumpObject



149
150
151
152
# File 'lib/card/cache.rb', line 149

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

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#fetch(key, &block) ⇒ Object



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

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

#read(key) ⇒ Object



124
125
126
127
# File 'lib/card/cache.rb', line 124

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

#resetObject



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

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

#write(key, value) ⇒ Object



129
130
131
132
# File 'lib/card/cache.rb', line 129

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