Class: Igniter::Extensions::Contracts::ContentAddressing::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/extensions/contracts/content_addressing/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



8
9
10
11
12
13
# File 'lib/igniter/extensions/contracts/content_addressing/cache.rb', line 8

def initialize
  @store = {}
  @hits = 0
  @misses = 0
  @mutex = Mutex.new
end

Instance Method Details

#clearObject



34
35
36
37
38
39
40
# File 'lib/igniter/extensions/contracts/content_addressing/cache.rb', line 34

def clear
  @mutex.synchronize do
    @store.clear
    @hits = 0
    @misses = 0
  end
end

#fetch(key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/igniter/extensions/contracts/content_addressing/cache.rb', line 15

def fetch(key)
  @mutex.synchronize do
    entry = @store[key.hex]
    if entry.nil?
      @misses += 1
      nil
    else
      @hits += 1
      entry
    end
  end
end

#sizeObject



42
43
44
# File 'lib/igniter/extensions/contracts/content_addressing/cache.rb', line 42

def size
  @mutex.synchronize { @store.size }
end

#statsObject



46
47
48
49
50
51
52
53
54
# File 'lib/igniter/extensions/contracts/content_addressing/cache.rb', line 46

def stats
  @mutex.synchronize do
    {
      size: @store.size,
      hits: @hits,
      misses: @misses
    }
  end
end

#store(key, value) ⇒ Object



28
29
30
31
32
# File 'lib/igniter/extensions/contracts/content_addressing/cache.rb', line 28

def store(key, value)
  @mutex.synchronize do
    @store[key.hex] = value
  end
end