Class: LruRedux::Cache
- Inherits:
-
Object
show all
- Defined in:
- lib/lru_redux/cache.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Cache
Returns a new instance of Cache.
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/lru_redux/cache.rb', line 7
def initialize(*args)
max_size, ignore_nil, _ = args
max_size ||= 1000
ignore_nil ||= false
validate_max_size!(max_size)
validate_ignore_nil!(ignore_nil)
@max_size = max_size
@ignore_nil = ignore_nil
@data_lru = {}
end
|
Instance Attribute Details
#ignore_nil ⇒ Object
Returns the value of attribute ignore_nil.
5
6
7
|
# File 'lib/lru_redux/cache.rb', line 5
def ignore_nil
@ignore_nil
end
|
#max_size ⇒ Object
Returns the value of attribute max_size.
5
6
7
|
# File 'lib/lru_redux/cache.rb', line 5
def max_size
@max_size
end
|
Instance Method Details
#[](key) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/lru_redux/cache.rb', line 63
def [](key)
key_found = true
value = @data_lru.delete(key) { key_found = false }
return unless key_found
@data_lru[key] = value
end
|
#[]=(key, val) ⇒ Object
71
72
73
|
# File 'lib/lru_redux/cache.rb', line 71
def []=(key, val)
store_item(key, val)
end
|
#clear ⇒ Object
99
100
101
|
# File 'lib/lru_redux/cache.rb', line 99
def clear
@data_lru.clear
end
|
#count ⇒ Object
Also known as:
length, size
103
104
105
|
# File 'lib/lru_redux/cache.rb', line 103
def count
@data_lru.size
end
|
#delete(key) ⇒ Object
Also known as:
evict
89
90
91
|
# File 'lib/lru_redux/cache.rb', line 89
def delete(key)
@data_lru.delete(key)
end
|
#each(&block) ⇒ Object
Also known as:
each_unsafe
75
76
77
|
# File 'lib/lru_redux/cache.rb', line 75
def each(&block)
@data_lru.to_a.reverse_each(&block)
end
|
#fetch(key) ⇒ Object
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/lru_redux/cache.rb', line 52
def fetch(key)
key_found = true
value = @data_lru.delete(key) { key_found = false }
if key_found
@data_lru[key] = value
else
yield if block_given? end
end
|
#getset(key) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/lru_redux/cache.rb', line 39
def getset(key)
key_found = true
value = @data_lru.delete(key) { key_found = false }
if key_found
@data_lru[key] = value
else
result = yield
store_item(key, result)
result
end
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?
94
95
96
|
# File 'lib/lru_redux/cache.rb', line 94
def key?(key)
@data_lru.key?(key)
end
|
#to_a ⇒ Object
81
82
83
|
# File 'lib/lru_redux/cache.rb', line 81
def to_a
@data_lru.to_a.reverse
end
|
#ttl=(_) ⇒ Object
28
29
30
|
# File 'lib/lru_redux/cache.rb', line 28
def ttl=(_)
nil
end
|
#values ⇒ Object
85
86
87
|
# File 'lib/lru_redux/cache.rb', line 85
def values
@data_lru.values.reverse
end
|