Class: Card::Cache::Persistent
Instance Attribute Summary collapse
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Class Method Summary collapse
Instance Method Summary collapse
- #annihilate ⇒ Object
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #fetch(key, &block) ⇒ Object
- #full_key(key) ⇒ Object
-
#initialize(opts) ⇒ Persistent
constructor
A new instance of Persistent.
- #new_stamp ⇒ Object
- #read(key) ⇒ Object
- #renew ⇒ Object
- #reset ⇒ Object
- #stamp ⇒ Object
- #stamp_key ⇒ Object
- #write(key, value) ⇒ Object
- #write_variable(key, variable, value) ⇒ Object
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
#prefix ⇒ Object
Returns the value of attribute prefix.
4 5 6 |
# File 'lib/card/cache/persistent.rb', line 4 def prefix @prefix end |
Class Method Details
Instance Method Details
#annihilate ⇒ Object
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
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_stamp ⇒ Object
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 |
#renew ⇒ Object
21 22 23 24 |
# File 'lib/card/cache/persistent.rb', line 21 def renew @stamp = nil @prefix = nil end |
#reset ⇒ Object
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 |
#stamp ⇒ Object
32 33 34 |
# File 'lib/card/cache/persistent.rb', line 32 def stamp @stamp ||= Cardio.cache.fetch stamp_key { new_stamp } end |
#stamp_key ⇒ Object
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 |