Class: LruRedux::TTL::Cache
- Inherits:
-
Object
- Object
- LruRedux::TTL::Cache
show all
- Defined in:
- lib/lru_redux/ttl/cache.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Cache
Returns a new instance of Cache.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/lru_redux/ttl/cache.rb', line 8
def initialize(*args)
max_size, ttl, ignore_nil = args
max_size ||= 1000
ttl ||= :none
ignore_nil ||= false
validate_max_size!(max_size)
validate_ttl!(ttl)
validate_ignore_nil!(ignore_nil)
@max_size = max_size
@ttl = ttl
@ignore_nil = ignore_nil
@data_lru = {}
@data_ttl = {}
end
|
Instance Attribute Details
#ignore_nil ⇒ Object
Returns the value of attribute ignore_nil.
6
7
8
|
# File 'lib/lru_redux/ttl/cache.rb', line 6
def ignore_nil
@ignore_nil
end
|
#max_size ⇒ Object
Returns the value of attribute max_size.
6
7
8
|
# File 'lib/lru_redux/ttl/cache.rb', line 6
def max_size
@max_size
end
|
#ttl ⇒ Object
Returns the value of attribute ttl.
6
7
8
|
# File 'lib/lru_redux/ttl/cache.rb', line 6
def ttl
@ttl
end
|
Instance Method Details
#[](key) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/lru_redux/ttl/cache.rb', line 80
def [](key)
evict_expired
key_found = true
value = @data_lru.delete(key) { key_found = false }
return unless key_found
@data_ttl.delete(key)
@data_ttl[key] = Time.now.to_f
@data_lru[key] = value
end
|
#[]=(key, val) ⇒ Object
92
93
94
95
96
|
# File 'lib/lru_redux/ttl/cache.rb', line 92
def []=(key, val)
evict_expired
store_item(key, val)
end
|
#clear ⇒ Object
132
133
134
135
|
# File 'lib/lru_redux/ttl/cache.rb', line 132
def clear
@data_ttl.clear
@data_lru.clear
end
|
#count ⇒ Object
Also known as:
length, size
137
138
139
140
141
|
# File 'lib/lru_redux/ttl/cache.rb', line 137
def count
evict_expired
@data_lru.size
end
|
#delete(key) ⇒ Object
Also known as:
evict
117
118
119
120
121
122
|
# File 'lib/lru_redux/ttl/cache.rb', line 117
def delete(key)
evict_expired
@data_ttl.delete(key)
@data_lru.delete(key)
end
|
#each(&block) ⇒ Object
Also known as:
each_unsafe
98
99
100
101
102
|
# File 'lib/lru_redux/ttl/cache.rb', line 98
def each(&block)
evict_expired
@data_lru.to_a.reverse_each(&block)
end
|
#expire ⇒ Object
145
146
147
|
# File 'lib/lru_redux/ttl/cache.rb', line 145
def expire
evict_expired
end
|
#fetch(key) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/lru_redux/ttl/cache.rb', line 65
def fetch(key)
evict_expired
key_found = true
value = @data_lru.delete(key) { key_found = false }
if key_found
@data_ttl.delete(key)
@data_ttl[key] = Time.now.to_f
@data_lru[key] = value
else
yield if block_given? end
end
|
#getset(key) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/lru_redux/ttl/cache.rb', line 48
def getset(key)
evict_expired
key_found = true
value = @data_lru.delete(key) { key_found = false }
if key_found
@data_ttl.delete(key)
@data_ttl[key] = Time.now.to_f
@data_lru[key] = value
else
result = yield
store_item(key, result)
result
end
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?
125
126
127
128
129
|
# File 'lib/lru_redux/ttl/cache.rb', line 125
def key?(key)
evict_expired
@data_lru.key?(key)
end
|
#to_a ⇒ Object
105
106
107
108
109
|
# File 'lib/lru_redux/ttl/cache.rb', line 105
def to_a
evict_expired
@data_lru.to_a.reverse
end
|
#values ⇒ Object
111
112
113
114
115
|
# File 'lib/lru_redux/ttl/cache.rb', line 111
def values
evict_expired
@data_lru.values.reverse
end
|