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.8"

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
30
31
# 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 = 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

#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



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

def [](key)
  lookup_key(key)
end

#[]=(key, value) ⇒ Object



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

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

#clearObject



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

def clear
  @storage.clear
end

#each(&block) ⇒ Object



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys
  @storage.keys    
end

#set(key, value, timestamp) ⇒ Object



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

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

#sizeObject



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

def size
  @storage.size
end

#timestamp?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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