Class: Persistent::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/persistent-cache.rb,
lib/persistent-cache/version.rb

Constant Summary collapse

STORAGE_SQLITE =
'sqlite'
STORAGE_DIRECTORY =
'directory'
STORAGE_RAM =
'ram'
FRESH =

Fresh is 1 day less than the bacula default job retention time. If this is configured differently, FRESH should be updated as well.

15465600
VERSION =
"0.3.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_details, fresh = FRESH, storage = STORAGE_SQLITE) ⇒ Cache

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/persistent-cache.rb', line 20

def initialize(storage_details, fresh = FRESH, storage = STORAGE_SQLITE)
  raise ArgumentError.new("No storage details provided") if storage_details.nil? or storage_details == ""

  @storage = StorageSQLite.new(storage_details) if storage == STORAGE_SQLITE
  @storage = StorageDirectory.new(storage_details) if storage == STORAGE_DIRECTORY
  @storage = StorageRAM.new if storage == STORAGE_RAM
  @fresh = fresh
  @storage_details = storage_details

  raise ArgumentError.new("Unsupported storage type #{storage}}") if @storage.nil?
end

Instance Attribute Details

#freshObject

Returns the value of attribute fresh.



18
19
20
# File 'lib/persistent-cache.rb', line 18

def fresh
  @fresh
end

#storageObject

Returns the value of attribute storage.



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

def storage
  @storage
end

#storage_detailsObject

Returns the value of attribute storage_details.



16
17
18
# File 'lib/persistent-cache.rb', line 16

def storage_details
  @storage_details
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  lookup_key(key)
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  if value.nil?
    delete_entry(key)
  else
    save_key_value_pair(key, value)
  end
end

#clearObject



66
67
68
# File 'lib/persistent-cache.rb', line 66

def clear
  @storage.clear
end

#each(&block) ⇒ Object



52
53
54
55
56
# File 'lib/persistent-cache.rb', line 52

def each(&block)
  keys.each do |key|
    yield key, lookup_key(key)
  end
end

#keysObject



62
63
64
# File 'lib/persistent-cache.rb', line 62

def keys
  @storage.keys    
end

#set(key, value, timestamp) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/persistent-cache.rb', line 32

def set(key, value, timestamp)
  if value.nil?
    delete_entry(key)
  else
    save_key_value_pair(key, value, timestamp)
  end
end

#sizeObject



58
59
60
# File 'lib/persistent-cache.rb', line 58

def size
  @storage.size
end

#timestamp?(key) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/persistent-cache.rb', line 70

def timestamp?(key)
  result = @storage.lookup_key(key)
  return nil if result.nil? or result[1].nil?
  Time.parse(result[1])
end