Class: Harbor::Cache

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

Defined Under Namespace

Classes: Disk, Item, Memory, PutArgumentError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Cache

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


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

def initialize(store)
  raise ArgumentError.new("Harbor::Cache.new expects a non-null 'store' parameter") unless store

  @store = store
  @semaphore = Mutex.new
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/harbor/cache.rb', line 10

def logger
  @logger
end

Instance Method Details

#delete(key) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/harbor/cache.rb', line 60

def delete(key)
  @semaphore.synchronize do
    @store.delete(key)
  end
rescue
  log("Harbor::Cache#put unable to delete cached content.", $!)
ensure
  nil
end

#delete_matching(key) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/harbor/cache.rb', line 70

def delete_matching(key)
  @semaphore.synchronize do
    @store.delete_matching(key)
  end
rescue
  log("Harbor::Cache#put unable to delete cached content.", $!)
ensure
  nil
end

#get(key) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/harbor/cache.rb', line 38

def get(key)
  if item = @store.get(key)
    if item.fresh?
      @semaphore.synchronize do
        @store.bump(key)
      end

      item
    else
      delete(key)

      item = nil
    end
  else
    item = nil
  end
rescue
  log("Harbor::Cache#get unable to retrieve cached content.", $!)
ensure
  defined?(item) ? item : nil
end

#put(key, content, ttl, maximum_age = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/harbor/cache.rb', line 19

def put(key, content, ttl, maximum_age = nil)
  raise PutArgumentError.new("Harbor::Cache::Memory#put expects a String value for 'key', got #{key}") unless key.is_a?(String)
  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
  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)
  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)

  @semaphore.synchronize do
    # Prevent multiple writes of similar content to the cache
    return true if (cached_item = @store.get(key)) && cached_item.fresh? && cached_item.content.hash == content.hash
    @store.put(key, ttl, maximum_age, content, Time.now)
  end
rescue
  log("Harbor::Cache#put unable to store cached content.", $!)

  raise if $!.is_a?(PutArgumentError)
ensure
  content
end