Class: Card::Cache::Persistent

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Persistent

Returns a new instance of Persistent.



14
15
16
17
18
19
# File 'lib/card/cache/persistent.rb', line 14

def initialize opts
  @store = opts[:store]
  @klass = opts[:class]
  @class_key = @klass.to_s.to_name.key
  @database = opts[:database] || self.class.database_name
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



4
5
6
# File 'lib/card/cache/persistent.rb', line 4

def prefix
  @prefix
end

Class Method Details

.database_nameObject



7
8
9
10
11
# File 'lib/card/cache/persistent.rb', line 7

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

Instance Method Details

#annihilateObject



76
77
78
# File 'lib/card/cache/persistent.rb', line 76

def annihilate
  @store.clear
end

#delete(key) ⇒ Object



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

def delete key
  @store.delete full_key(key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/card/cache/persistent.rb', line 80

def exist? key
  @store.exist? full_key(key)
end

#fetch(key, &block) ⇒ Object



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

def fetch key, &block
  @store.fetch full_key(key), &block
end

#full_key(key) ⇒ Object



48
49
50
# File 'lib/card/cache/persistent.rb', line 48

def full_key key
  "#{prefix}/#{key}"
end

#new_stampObject



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

def new_stamp
  Time.now.to_i.to_s 32
end

#read(key) ⇒ Object



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

def read key
  @store.read full_key(key)
end

#renewObject



21
22
23
24
# File 'lib/card/cache/persistent.rb', line 21

def renew
  @stamp = nil
  @prefix = nil
end

#resetObject



26
27
28
29
30
# File 'lib/card/cache/persistent.rb', line 26

def reset
  @stamp = new_stamp
  @prefix = nil
  Cardio.cache.write stamp_key, @stamp
end

#stampObject



32
33
34
# File 'lib/card/cache/persistent.rb', line 32

def stamp
  @stamp ||= Cardio.cache.fetch stamp_key { new_stamp }
end

#stamp_keyObject



36
37
38
# File 'lib/card/cache/persistent.rb', line 36

def stamp_key
  "#{@database}/#{@class_key}/stamp"
end

#write(key, value) ⇒ Object



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

def write key, value
  @store.write full_key(key), value
end

#write_variable(key, variable, value) ⇒ Object



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

def write_variable key, variable, value
  if @store && (object = read key)
    object.instance_variable_set "@#{variable}", value
    write key, object
  end
  value
end