Class: Persistent::Cache

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
# File 'lib/persistent-cache.rb', line 17

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

  @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.



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

def encoding
  @encoding
end

#freshObject

Returns the value of attribute fresh.



14
15
16
# File 'lib/persistent-cache.rb', line 14

def fresh
  @fresh
end

#storageObject

Returns the value of attribute storage.



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

def storage
  @storage
end

#storage_detailsObject

Returns the value of attribute storage_details.



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

def storage_details
  @storage_details
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
# File 'lib/persistent-cache.rb', line 43

def [](key)
  lookup_key(key)
end

#[]=(key, value) ⇒ Object



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

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

#clearObject



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

def clear
  @storage.clear
end

#each(&_block) ⇒ Object



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys
  @storage.keys    
end

#set(key, value, timestamp) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/persistent-cache.rb', line 27

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

#sizeObject



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

def size
  @storage.size
end

#timestamp?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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