Class: Opod::FileCache

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

Instance Method Summary collapse

Constructor Details

#initialize(name = "cache", keepalive = nil) ⇒ FileCache

Returns a new instance of FileCache.



11
12
13
14
15
16
# File 'lib/opod/file.rb', line 11

def initialize(name = "cache", keepalive = nil)
  @path = File.join(FileCache.basedir, name)
  @keepalive = keepalive

  FileUtils.mkdir_p(@path, :mode => 0700)
end

Instance Method Details

#[](k) ⇒ Object Also known as: get



24
25
26
27
28
# File 'lib/opod/file.rb', line 24

def [](k)
  fn = File.join(@path, escape_filename(k.to_s) )
  return nil unless File.exists?(fn)
  decode_file(fn)
end

#[]=(k, v) ⇒ Object Also known as: set



18
19
20
21
# File 'lib/opod/file.rb', line 18

def []=(k,v)
  fn = File.join(@path, escape_filename(k.to_s) )
  encode_file(fn, v)
end

#allObject



46
47
48
# File 'lib/opod/file.rb', line 46

def all
  Dir.glob( File.join(@path, '*' ) )
end

#delete(k) ⇒ Object



31
32
33
34
# File 'lib/opod/file.rb', line 31

def delete(k)
  f = File.join(@path, escape_filename(k.to_s))
  File.delete(f) if File.exists?(f)
end

#gc!Object



36
37
38
39
40
41
42
43
44
# File 'lib/opod/file.rb', line 36

def gc!
  return unless @keepalive

  now = Time.now
  all.each do |fn|
    expire_time = File.stat(fn).atime + @keepalive
    File.delete(fn) if now > expire_time
  end
end