Class: ZendeskAPI::LRUCache

Inherits:
Object
  • Object
show all
Defined in:
lib/zendesk_api/lru_cache.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = 10) ⇒ LRUCache

Returns a new instance of LRUCache.



7
8
9
10
11
# File 'lib/zendesk_api/lru_cache.rb', line 7

def initialize(size = 10)
  @size = size
  @store = {}
  @lru = []
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/zendesk_api/lru_cache.rb', line 5

def size
  @size
end

Instance Method Details

#fetch(key) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/zendesk_api/lru_cache.rb', line 25

def fetch(key)
  if @store.has_key? key
    read key
  else
    write key, yield
  end
end

#read(key) ⇒ Object



20
21
22
23
# File 'lib/zendesk_api/lru_cache.rb', line 20

def read(key)
  set_lru(key)
  @store[key]
end

#write(key, value) ⇒ Object



13
14
15
16
17
18
# File 'lib/zendesk_api/lru_cache.rb', line 13

def write(key, value)
  @store[key] = value
  set_lru(key)
  @store.delete(@lru.pop) if @lru.size > @size
  value
end