Harbor Documentation

Public Class Methods

new(path)

     # File lib/harbor/cache/disk.rb, line 3
3:   def initialize(path)
4:     @path = path.is_a?(Pathname) ? path : Pathname(path)
5: 
6:     FileUtils.mkdir_p(@path) unless ::File.directory?(@path.to_s)
7:   end

Public Instance Methods

[](key)

Alias for get

bump(key)

      # File lib/harbor/cache/disk.rb, line 47
47:   def bump(key)
48:     if item = get(key)
49:       current_path = path_for_item(item)
50: 
51:       item.bump
52: 
53:       new_path = path_for_item(item)
54:       FileUtils.mv(current_path, new_path) unless current_path == new_path
55:     end
56:   end

delete(key)

      # File lib/harbor/cache/disk.rb, line 35
35:   def delete(key)
36:     if (path = filename_for_key(key))
37:       FileUtils.rm(path) rescue nil
38:     end
39:   end

delete_matching(key)

      # File lib/harbor/cache/disk.rb, line 41
41:   def delete_matching(key)
42:     filenames_for_key(key).each do |path|
43:       FileUtils.rm(path) rescue nil
44:     end
45:   end

get(key)

      # File lib/harbor/cache/disk.rb, line 9
 9:   def get(key)
10:     if (path = filename_for_key(key))
11:       item_for_path(path)
12:     else
13:       nil
14:     end
15:   end
Also aliased as: []

keys_matching(key_regex)

      # File lib/harbor/cache/disk.rb, line 19
19:   def keys_matching(key_regex)
20:     Dir[@path + "*"].select { |path| path[/.*?__INFO__/] }.select{|path| path[key_regex]}
21:   end

put(key, ttl, maximum_age, content, cached_at)

      # File lib/harbor/cache/disk.rb, line 23
23:   def put(key, ttl, maximum_age, content, cached_at)
24:     item = Harbor::Cache::Item.new(key, ttl, maximum_age, content, cached_at)
25: 
26:     FileUtils.rm(filenames_for_key(key))
27: 
28:     ::File.open(path_for_item(item), 'w') do |file|
29:       file.write(content)
30:     end
31: 
32:     item
33:   end

Private Instance Methods

filename_for_key(key)

      # File lib/harbor/cache/disk.rb, line 68
68:   def filename_for_key(key)
69:     filenames_for_key(key).first
70:   end

filenames_for_key(key)

      # File lib/harbor/cache/disk.rb, line 60
60:   def filenames_for_key(key)
61:     if key.is_a?(Regexp)
62:       Dir[@path + "*"].select { |path| path[/.*?__INFO__/] =~ key }
63:     else
64:       Dir[@path + "c_#{key}.*"]
65:     end
66:   end

item_for_path(path)

      # File lib/harbor/cache/disk.rb, line 72
72:   def item_for_path(path)
73:     components = ::File.basename(path).split('.')
74: 
75:     return nil if components.size < 6
76: 
77:     expires_at = Time.parse(components.pop)
78:     cached_at = Time.parse(components.pop)
79: 
80:     if (maximum_age = components.pop).size > 0
81:       maximum_age = maximum_age.to_i
82:     else
83:       maximum_age = nil
84:     end
85: 
86:     if (ttl = components.pop).size > 0
87:       ttl = ttl.to_i
88:     else
89:       ttl = nil
90:     end
91: 
92:     key = components.reject { |c| c == "__INFO__" }.join('.').sub(/^c\_/, '')
93: 
94:     Harbor::Cache::Item.new(key, ttl, maximum_age, Pathname(path), cached_at, expires_at)
95:   end

path_for_item(item)

      # File lib/harbor/cache/disk.rb, line 97
97:   def path_for_item(item)
98:     @path + "c_#{item.key}.__INFO__.#{item.ttl}.#{item.maximum_age}.#{item.cached_at.strftime('%Y%m%dT%H%M%S%Z')}.#{item.expires_at.strftime('%Y%m%dT%H%M%S%Z')}"
99:   end