-
Notifications
You must be signed in to change notification settings - Fork 1
/
lru_cache.rb
37 lines (31 loc) · 911 Bytes
/
lru_cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# set operations
# if cache is empty, then add the current content in the first node also update the hash with page_number and content
# else check to see if cache already exists, if it does, then remove it from the linked_list and add it back to the head
# if it does not exist, check to see if the maximum size of linked_list has reached, if it is reached, remove the rear element
# get operations
# read the content based on the page number
class LRU
def initialize
@cache = LinkedList.new
@hash = Hash.new
end
def set(page, content)
node = Node.new(value: content)
if @cache.empty?
@hash[page] = node
@cache.head = node
end
if @hash[page]
c = @hash[page]
c.previous.next = c.next
else
if @cache.size > 10
@cache[-1].next = nil
end
end
@cache.next = node
end
def get(page)
@hash[page].value
end
end