Class: Persistent::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/persistent-cache.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

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)


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

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

  @storage = create_storage(storage, storage_details)
  @fresh = fresh
  @storage_details = storage_details

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

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



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

def encoding
  @encoding
end

#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



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

def [](key)
  lookup_key(key)
end

#[]=(key, value) ⇒ Object



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

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

#clearObject



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

def clear
  @storage.clear
end

#each(&_block) ⇒ Object



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
# File 'lib/persistent-cache.rb', line 76

def key?(key)
  if @storage.keys
    @storage.keys.each do |k|
      return k if k == key
    end
  end
  return nil      
end

#keysObject



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

def keys
  @storage.keys    
end

#set(key, value, timestamp) ⇒ Object



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

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

#sizeObject



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

def size
  @storage.size
end

#timestamp?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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