Harbor Documentation

Public Class Methods

new(path)

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

Public Instance Methods

[](key)

Alias for get

bump(key)

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

delete(key)

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

delete_matching(key)

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

get(key)

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

keys_matching(key_regex)

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

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

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

Private Instance Methods

filename_for_key(key)

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

filenames_for_key(key)

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

item_for_path(path)

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

path_for_item(item)

       # File lib/harbor/cache/disk.rb, line 99
 99:   def path_for_item(item)
100:     @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')}"
101:   end