Class: Githu3::Cache::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/githu3/cache/disk.rb

Constant Summary collapse

DEFAULTS =
{ 
  :path       => '/tmp/githu3',
  :namespace  => 'cache',
  :expire     => 120
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Disk

Returns a new instance of Disk.



14
15
16
17
18
# File 'lib/githu3/cache/disk.rb', line 14

def initialize opts={}
  @config = DEFAULTS.merge(opts || {})
  @path = ::File.expand_path(::File.join(@config[:path], @config[:namespace]))
  FileUtils.mkdir_p @path
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/githu3/cache/disk.rb', line 6

def config
  @config
end

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/githu3/cache/disk.rb', line 6

def store
  @store
end

Instance Method Details

#get(k) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/githu3/cache/disk.rb', line 20

def get k
  val = nil
  file_path = ::File.join(@path, k)
  if ::File.exists?(file_path)
    if (Time.now - ::File.atime(file_path)) < @config[:expire]
      val = Marshal.load(::File.read(file_path))
    else
      FileUtils.rm(file_path)
    end
  else
  end
  val
end

#set(k, val, opts = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/githu3/cache/disk.rb', line 34

def set k, val, opts={}
  file_path = ::File.join(@path, k)
  f = open(file_path, 'wb')
  f.write(Marshal.dump(val))
  f.close
end