Class: Card::Cache::Persistent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Persistent

Returns a new instance of Persistent.



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

def initialize opts
  @store = opts[:store]
  self.system_prefix =
    opts[:prefix] || Card::Cache.system_prefix(opts[:class])
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Instance Method Details

#delete(key) ⇒ Object



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

def delete key
  @store.delete @prefix + key
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def exist? key
  @store.exist? @prefix + key
end

#fetch(key, &block) ⇒ Object



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

def fetch key, &block
  @store.fetch @prefix + key, &block
end

#read(key) ⇒ Object



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

def read key
  @store.read(@prefix + key)
end

#resetObject



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

def reset
  @store.clear
rescue => e
  Rails.logger.debug "Problem clearing cache: #{e.message}"
end

#system_prefix=(system_prefix) ⇒ Object



12
13
14
15
# File 'lib/card/cache/persistent.rb', line 12

def system_prefix= system_prefix
  @system_prefix = system_prefix
  @prefix = "#{system_prefix}/"
end

#write(key, value) ⇒ Object



29
30
31
# File 'lib/card/cache/persistent.rb', line 29

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

#write_variable(key, variable, value) ⇒ Object



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

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