Class: Harbor::Cache::Disk

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Disk

Returns a new instance of Disk.



5
6
7
8
9
# File 'lib/harbor/cache/disk.rb', line 5

def initialize(path)
  @path = path.is_a?(Pathname) ? path : Pathname(path)

  FileUtils.mkdir_p(@path) unless ::File.directory?(@path.to_s)
end

Instance Method Details

#bump(key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/harbor/cache/disk.rb', line 45

def bump(key)
  if item = get(key)
    current_path = path_for_item(item)

    item.bump

    new_path = path_for_item(item)
    FileUtils.mv(current_path, new_path) unless current_path == new_path
  end
end

#delete(key) ⇒ Object



33
34
35
36
37
# File 'lib/harbor/cache/disk.rb', line 33

def delete(key)
  if (path = filename_for_key(key))
    FileUtils.rm(path) rescue nil
  end
end

#delete_matching(key) ⇒ Object



39
40
41
42
43
# File 'lib/harbor/cache/disk.rb', line 39

def delete_matching(key)
  filenames_for_key(key).each do |path|
    FileUtils.rm(path) rescue nil
  end
end

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



11
12
13
14
15
16
17
# File 'lib/harbor/cache/disk.rb', line 11

def get(key)
  if (path = filename_for_key(key))
    item_for_path(path)
  else
    nil
  end
end

#put(key, ttl, maximum_age, content, cached_at) ⇒ Object



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

def put(key, ttl, maximum_age, content, cached_at)
  item = Harbor::Cache::Item.new(key, ttl, maximum_age, content, cached_at)

  FileUtils.rm(filenames_for_key(key))

  ::File.open(path_for_item(item), 'w') do |file|
    file.write(content)
  end

  item
end