Class: FileCache

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

Instance Attribute Summary

Attributes inherited from Cache

#expiry_time, #max_size, #refresh, #store

Instance Method Summary collapse

Methods inherited from Cache

#check_refresh, #exists?, #has_refresh?

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
# File 'lib/libcache/file_cache.rb', line 26

def get(key)
  refresh
  return File.read(File.join(store, key.to_s))
end

#invalidate(key) ⇒ Object



31
32
33
34
# File 'lib/libcache/file_cache.rb', line 31

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

#invalidateAllObject



36
37
38
39
40
41
# File 'lib/libcache/file_cache.rb', line 36

def invalidateAll
  super
  Dir.foreach(store) do |f|
    File.delete(File.join(store, f)) if f != '.' && f != '..'
  end
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