Harbor Documentation

Attributes

  • logger [RW] (Not documented)

Public Class Methods

new(store)

      # File lib/harbor/cache.rb, line 12
12:     def initialize(store)
13:       raise ArgumentError.new("Harbor::Cache.new expects a non-null 'store' parameter") unless store
14:       logger.debug "INIT: #{store.inspect}" if logger
15: 
16:       @store = store
17:       @semaphore = Mutex.new
18:     end

Public Instance Methods

delete(key)

      # File lib/harbor/cache.rb, line 65
65:     def delete(key)
66:       logger.debug "DELETE: #{key.inspect}" if logger
67:       @semaphore.synchronize do
68:         @store.delete(key)
69:       end
70:     rescue
71:       log("Harbor::Cache#put unable to delete cached content.", $!)
72:     ensure
73:       nil
74:     end

delete_matching(key)

      # File lib/harbor/cache.rb, line 76
76:     def delete_matching(key)
77:       logger.debug "DELETE MATCHING: #{key.inspect}" if logger
78:       @semaphore.synchronize do
79:         @store.delete_matching(key)
80:       end
81:     rescue
82:       log("Harbor::Cache#put unable to delete cached content.", $!)
83:     ensure
84:       nil
85:     end

get(key)

      # File lib/harbor/cache.rb, line 41
41:     def get(key)
42:       if item = @store.get(key)
43:         logger.debug "HIT: #{key.inspect}" if logger
44:         if item.fresh?
45:           logger.debug "BUMP: #{key.inspect}" if logger
46:           @semaphore.synchronize do
47:             @store.bump(key)
48:           end
49: 
50:           item
51:         else
52:           delete(key)
53: 
54:           item = nil
55:         end
56:       else
57:         item = nil
58:       end
59:     rescue
60:       log("Harbor::Cache#get unable to retrieve cached content.", $!)
61:     ensure
62:       defined?(item) ? item : nil
63:     end

put(key, content, ttl, maximum_age = nil)

      # File lib/harbor/cache.rb, line 20
20:     def put(key, content, ttl, maximum_age = nil)
21:       raise PutArgumentError.new("Harbor::Cache::Memory#put expects a String value for 'key', got #{key}") unless key.is_a?(String)
22:       raise PutArgumentError.new("Harbor::Cache::Memory#put expects a Fixnum value greater than 0 for 'ttl', got #{ttl}") unless ttl.is_a?(Fixnum) && ttl > 0
23:       raise PutArgumentError.new("Harbor::Cache::Memory#put expects nil, or a Fixnum value greater than 0 for 'maximum_age', got #{maximum_age}") unless maximum_age.nil? || (maximum_age.is_a?(Fixnum) && maximum_age > 0)
24:       raise PutArgumentError.new("Harbor::Cache::Memory#put expects a maximum_age greater than the ttl, got ttl: #{ttl}, maximum_age: #{maximum_age}") if maximum_age && ttl && (maximum_age <= ttl)
25: 
26:       @semaphore.synchronize do
27:         # Prevent multiple writes of similar content to the cache
28:         return true if (cached_item = @store.get(key)) && cached_item.fresh? && cached_item.content.hash == content.hash
29: 
30:         logger.debug "PUT: #{key}  (ttl:#{ttl.inspect} maximum_age:#{maximum_age.inspect})" if logger
31:         @store.put(key, ttl, maximum_age, content, Time.now)
32:       end
33:     rescue
34:       log("Harbor::Cache#put unable to store cached content.", $!)
35: 
36:       raise if $!.is_a?(PutArgumentError)
37:     ensure
38:       content
39:     end

Private Instance Methods

log(message, error)

      # File lib/harbor/cache.rb, line 89
89:     def log(message, error)
90:       if @logger
91:         @logger.fatal("#{message} #{$!}\n#{$!.message}\nBacktrace:\n#{$!.backtrace.join("\n")}")
92:       end
93:     end