Class: ThreadSafeLru::LruCache

Inherits:
Object
  • Object
show all
Defined in:
lib/threadsafe-lru/ThreadSafeLruCache.rb

Instance Method Summary collapse

Constructor Details

#initialize(size, &block) ⇒ LruCache

Returns a new instance of LruCache.



6
7
8
9
10
11
12
# File 'lib/threadsafe-lru/ThreadSafeLruCache.rb', line 6

def initialize size, &block
  @size=size
  @cached_values={}
  @factory_block=block
  @recently_used=ThreadSafeLru::DoubleLinkedList.new
  @lock=Mutex.new
end

Instance Method Details

#clearObject



28
29
30
31
32
33
# File 'lib/threadsafe-lru/ThreadSafeLruCache.rb', line 28

def clear
  @lock.synchronize do
    @cached_values.clear
    @recently_used.clear
  end
end

#drop(key) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/threadsafe-lru/ThreadSafeLruCache.rb', line 19

def drop key
  @lock.synchronize do
    node=@cached_values.delete key
    node.remove if node
      
  end
end

#get(key, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/threadsafe-lru/ThreadSafeLruCache.rb', line 35

def get key, &block
  node=nil
  @lock.synchronize do
    node=get_node(key)
  end
  node.get_value(block ? block : @factory_block)
end

#sizeObject



14
15
16
# File 'lib/threadsafe-lru/ThreadSafeLruCache.rb', line 14

def size
  @cached_values.size
end