Class: FileCache

Inherits:
Cache show all
Defined in:
lib/libcache/file_cache.rb

Instance Attribute Summary

Attributes inherited from Cache

#expiry_time, #refresh, #store

Instance Method Summary collapse

Constructor Details

#initializeFileCache

Returns a new instance of FileCache.



5
6
7
# File 'lib/libcache/file_cache.rb', line 5

def initialize
  super
end

Instance Method Details

#create_storeObject



9
10
11
12
13
14
# File 'lib/libcache/file_cache.rb', line 9

def create_store
  @cache = Hash.new
  if store == nil
    raise 'Store path is missing!'
  end
end

#get(key) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/libcache/file_cache.rb', line 26

def get(key)
  if @cache[key] == nil
    val = refresh.call(key)
    put(key, val)
    return val
  end
  return File.read(File.join(store, key.to_s))
end

#invalidate(key) ⇒ Object



35
36
37
38
# File 'lib/libcache/file_cache.rb', line 35

def invalidate(key)
  super
  File.delete(File.join(store, key.to_s))
end

#invalidateAllObject



40
41
42
43
44
45
# File 'lib/libcache/file_cache.rb', line 40

def invalidateAll
  super
  Dir.foreach(store) { |f|
    File.delete(File.join(store, f)) if f != '.' && f != '..'
  }
end

#put(key, value) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/libcache/file_cache.rb', line 16

def put(key, value)
  @cache[key] = value
  File.open(File.join(store, key.to_s), 'w') do |f|
    f.write(value)
  end
  @scheduler.in expiry_time, :blocking => true do
    invalidate key
  end
end