Class: CacheLib::LirsCache
Instance Attribute Summary
Attributes inherited from BasicCache
#limit
Instance Method Summary
collapse
Methods inherited from BasicCache
#each, #expire, #key?, #keys, #size, #to_a, #values
Constructor Details
#initialize(*args) ⇒ LirsCache
Returns a new instance of LirsCache.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/cache_lib/lirs_cache.rb', line 3
def initialize(*args)
s_limit, q_limit = args
fail ArgumentError, 'S Limit must be 1 or greater.' if
s_limit.nil? || s_limit < 1
fail ArgumentError, 'Q Limit must be 1 or greater.' if
q_limit.nil? || q_limit < 1
@s_limit = s_limit
@q_limit = q_limit
@limit = s_limit + q_limit
@cache = UtilHash.new
@stack = UtilHash.new
@queue = UtilHash.new
end
|
Instance Method Details
#clear ⇒ Object
92
93
94
95
96
97
|
# File 'lib/cache_lib/lirs_cache.rb', line 92
def clear
@cache.clear
@stack.clear
@queue.clear
nil
end
|
#evict(key) ⇒ Object
Also known as:
delete
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/cache_lib/lirs_cache.rb', line 76
def evict(key)
return unless @cache.key?(key)
value = @cache.delete(key)
if @queue.key?(key)
@queue.delete(key)
else
promote_hir if @queue.size > 0
trim_stack
end
value
end
|
#fetch(key) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/cache_lib/lirs_cache.rb', line 68
def fetch(key)
if @cache.key?(key)
hit(key)
else
yield if block_given?
end
end
|
#get(key) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/cache_lib/lirs_cache.rb', line 47
def get(key)
if @cache.key?(key)
hit(key)
else
miss(key, yield)
end
end
|
#initialize_copy(source) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/cache_lib/lirs_cache.rb', line 20
def initialize_copy(source)
source_raw = source.raw
@limit = source_raw[:limit]
@s_limit = source_raw[:s_limit]
@q_limit = source_raw[:q_limit]
@cache = source_raw[:cache]
@stack = source_raw[:stack]
@queue = source_raw[:queue]
end
|
#inspect ⇒ Object
108
109
110
111
112
113
114
|
# File 'lib/cache_lib/lirs_cache.rb', line 108
def inspect
"#{self.class}, "\
"Limit: #{@limit}, "\
"Stack Limit: #{@s_limit}, "\
"Queue Limit: #{@q_limit}, "\
"Size: #{@cache.size}"
end
|
#limit=(args) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/cache_lib/lirs_cache.rb', line 32
def limit=(args)
s_limit, q_limit = args
fail ArgumentError, 'S Limit must be 1 or greater.' if
s_limit.nil? || s_limit < 1
fail ArgumentError, 'Q Limit must be 1 or greater.' if
q_limit.nil? || q_limit < 1
@s_limit = s_limit
@q_limit = q_limit
@limit = s_limit + q_limit
resize
end
|
#lookup(key) ⇒ Object
Also known as:
[]
64
65
66
|
# File 'lib/cache_lib/lirs_cache.rb', line 64
def lookup(key)
hit(key) if @cache.key?(key)
end
|
#raw ⇒ Object
99
100
101
102
103
104
105
106
|
# File 'lib/cache_lib/lirs_cache.rb', line 99
def raw
{ limit: @limit,
s_limit: @s_limit,
q_limit: @q_limit,
cache: @cache.clone,
stack: @stack.clone,
queue: @queue.clone }
end
|
#store(key, value) ⇒ Object
Also known as:
[]=
55
56
57
58
59
60
61
62
|
# File 'lib/cache_lib/lirs_cache.rb', line 55
def store(key, value)
if @cache.key?(key)
@cache[key] = value
hit(key)
else
miss(key, value)
end
end
|