Class: Jerakia::Cache::File

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/cache/file.rb

Class Method Summary collapse

Class Method Details

.add(index, data) ⇒ Object


16
17
18
19
20
# File 'lib/jerakia/cache/file.rb', line 16

def add(index, data)
  filestate = state(index)
  Jerakia.log.debug("Adding #{index} to file cache with state #{filestate}")
  cache.add(index, data, :state => filestate) if filestate
end

.cacheObject


5
6
7
# File 'lib/jerakia/cache/file.rb', line 5

def cache
  Jerakia::Cache
end

.get(index) ⇒ Object


59
60
61
# File 'lib/jerakia/cache/file.rb', line 59

def get(index)
  cache.get(index)
end

.import_file(filename) ⇒ Object


39
40
41
42
# File 'lib/jerakia/cache/file.rb', line 39

def import_file(filename)
  Jerakia.log.debug("Importing file #{filename} to file cache")
  File.read(filename)
end

.retrieve(filename) ⇒ Object

If the cache has a valid copy of the file, then we retrieve it, if the cache doesn't have a copy, or if the state has changed, then we should add it to the cache again and overite the existing data.

Returns nil if the file doesn't exist


50
51
52
53
54
55
56
57
# File 'lib/jerakia/cache/file.rb', line 50

def retrieve(filename)
  if valid?(filename)
    Jerakia.log.debug("Using cached contents of #{filename}")
    get(filename)
  else
    add(filename, import_file(filename)) if File.exists?(filename)
  end
end

.state(filename) ⇒ Object

Returns the latest mtime of the file if the file exists and nil if it doesn't exist.


12
13
14
# File 'lib/jerakia/cache/file.rb', line 12

def state(filename)
  ::File.stat(filename).mtime if ::File.exist?(filename)
end

.valid?(index) ⇒ Boolean

Returns:

  • (Boolean)

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jerakia/cache/file.rb', line 22

def valid?(index)
  if cache.in_bucket?(index)
    unless File.exists?(index)
      cache.purge(index)
      return false
    end
     = cache.(index)
    if 
      [:state] == state(index)
    else
      false
    end
  else
    false
  end
end