Class: Flannel::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/flannel/file_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_location, seconds_till_expiration = 3600) ⇒ FileCache

Returns a new instance of FileCache.



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

def initialize cache_location, seconds_till_expiration=3600
  unless File.exists?(cache_location) && File.directory?(cache_location)
	raise CacheLocationDoesNotExistError
  end
  
  @cache_location = cache_location
  @seconds_till_expiration = seconds_till_expiration
end

Instance Attribute Details

#seconds_till_expirationObject (readonly)

Returns the value of attribute seconds_till_expiration.



6
7
8
# File 'lib/flannel/file_cache.rb', line 6

def seconds_till_expiration
  @seconds_till_expiration
end

Instance Method Details

#retrieve(url) ⇒ Object



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

def retrieve url
  key = generate_key url
  file = File.join(@cache_location, key)
  
  return nil unless File.exists?(file)
 
  saved_at = File.mtime(file)
  expires_at = saved_at + @seconds_till_expiration
  
  if Time.now >= expires_at
	File.delete(file)
	return nil
  end
  
  read file     
end

#save(url, data) ⇒ Object



17
18
19
20
# File 'lib/flannel/file_cache.rb', line 17

def save url, data
  key = generate_key url
  File.open(File.join(@cache_location, key), 'w') {|f| f.write(data) }
end