caches 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +4 -3
- data/lib/caches/lru.rb +11 -2
- data/lib/caches/stats.rb +4 -3
- data/lib/caches/ttl.rb +11 -2
- data/lib/caches/version.rb +1 -1
- data/spec/caches/lru_spec.rb +22 -0
- data/spec/caches/ttl_spec.rb +22 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 443cc1eff19b292441d504e849ba7cfe09931f03
|
4
|
+
data.tar.gz: 18ab2f338cada16695d69e18452f223c276c7050
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7902d5bb624352d4c0122d247f09ba28dfe41ca68f3041bccc83f7035799102299b2c084d7a2a5bb98071358febc3bff51c7b59516182d6bcb89c55e738c5191
|
7
|
+
data.tar.gz: 38f8cdec9b5e46634c2cdd9b4124a6f17c70171813fcecc56e1c2242961b359d27bb47273c0a3edb6f4c4a5e3cfc0ebda9d4f4e2c7a7a89547ea24abc5ad61f8
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
|
6
6
|
- Nothing
|
7
7
|
|
8
|
+
## 0.0.7 - 2015-03-09
|
9
|
+
|
10
|
+
- `#clear` method clears a cache's data and stats
|
11
|
+
- Bugfix for `#stats`: 0 hits and 0 misses is `0%`, not `NaN`
|
12
|
+
|
8
13
|
## 0.0.6 - 2015-03-09
|
9
14
|
|
10
15
|
- Stats include a percentage
|
@@ -13,7 +18,7 @@ All notable changes to this project will be documented in this file.
|
|
13
18
|
|
14
19
|
### Added
|
15
20
|
|
16
|
-
-
|
21
|
+
- `#stats` method gives counts of how many cache hits and misses have occurred
|
17
22
|
|
18
23
|
## 0.0.4 - 2015-03-03
|
19
24
|
|
data/README.md
CHANGED
@@ -63,9 +63,10 @@ puts h[:a] # => nil
|
|
63
63
|
|
64
64
|
## Methods Common to All Caches
|
65
65
|
|
66
|
-
- `
|
67
|
-
- `
|
68
|
-
- `
|
66
|
+
- `#fetch` works like `Hash#fetch`
|
67
|
+
- `#memoize` returns the key's value, if any; if not, it gets its return value from the block, and sets the key before it returns
|
68
|
+
- `#stats` tells how many cache hits and misses have occurred
|
69
|
+
- `#clear` resets all data and stats
|
69
70
|
|
70
71
|
## Installation
|
71
72
|
|
data/lib/caches/lru.rb
CHANGED
@@ -13,8 +13,7 @@ module Caches
|
|
13
13
|
|
14
14
|
def initialize(options = {})
|
15
15
|
self.max_keys = options.fetch(:max_keys, 20)
|
16
|
-
|
17
|
-
self.keys = LinkedList.new
|
16
|
+
initialize_data
|
18
17
|
end
|
19
18
|
|
20
19
|
def [](key)
|
@@ -45,8 +44,18 @@ module Caches
|
|
45
44
|
keys.length
|
46
45
|
end
|
47
46
|
|
47
|
+
def clear
|
48
|
+
initialize_data
|
49
|
+
end
|
50
|
+
|
48
51
|
private
|
49
52
|
|
53
|
+
def initialize_data
|
54
|
+
self.data = {}
|
55
|
+
self.keys = LinkedList.new
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
50
59
|
def prune
|
51
60
|
return unless keys.length > max_keys
|
52
61
|
expiring_node = keys.pop
|
data/lib/caches/stats.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Caches
|
2
2
|
module Stats
|
3
3
|
|
4
|
-
def
|
4
|
+
def initialize_data
|
5
5
|
@hits = 0
|
6
6
|
@misses = 0
|
7
7
|
super
|
@@ -22,8 +22,9 @@ module Caches
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def hit_rate
|
25
|
-
|
26
|
-
|
25
|
+
attempts = Float(@hits) + Float(@misses)
|
26
|
+
ratio = attempts.zero? ? 0.0 : Float(@hits) / attempts
|
27
|
+
"#{ratio * 100}%"
|
27
28
|
end
|
28
29
|
|
29
30
|
end
|
data/lib/caches/ttl.rb
CHANGED
@@ -15,9 +15,8 @@ module Caches
|
|
15
15
|
def initialize(options = {})
|
16
16
|
self.ttl = options.fetch(:ttl) { 3600 }
|
17
17
|
self.refresh = !!(options.fetch(:refresh, false))
|
18
|
-
self.data = {}
|
19
|
-
self.nodes = LinkedList.new
|
20
18
|
self.max_keys = options[:max_keys]
|
19
|
+
initialize_data
|
21
20
|
end
|
22
21
|
|
23
22
|
def [](key)
|
@@ -70,8 +69,18 @@ module Caches
|
|
70
69
|
data.values.map { |h| h.fetch(:value) }
|
71
70
|
end
|
72
71
|
|
72
|
+
def clear
|
73
|
+
initialize_data
|
74
|
+
end
|
75
|
+
|
73
76
|
private
|
74
77
|
|
78
|
+
def initialize_data
|
79
|
+
self.data = {}
|
80
|
+
self.nodes = LinkedList.new
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
75
84
|
def current?(key)
|
76
85
|
(current_time - data[key][:time]) < ttl
|
77
86
|
end
|
data/lib/caches/version.rb
CHANGED
data/spec/caches/lru_spec.rb
CHANGED
@@ -134,5 +134,27 @@ describe Caches::LRU do
|
|
134
134
|
|
135
135
|
end
|
136
136
|
|
137
|
+
describe "clear" do
|
138
|
+
|
139
|
+
it "clears all cached data" do
|
140
|
+
cache.clear
|
141
|
+
expect(cache.size).to eq(0)
|
142
|
+
end
|
143
|
+
|
144
|
+
it "clears the stats" do
|
145
|
+
cache[:a]
|
146
|
+
cache[:nope]
|
147
|
+
cache.clear
|
148
|
+
expect(cache.stats).to eq(hits: 0, misses: 0, hit_rate: "0.0%")
|
149
|
+
end
|
150
|
+
|
151
|
+
it "returns the empty cache" do
|
152
|
+
id = cache.object_id
|
153
|
+
returned = cache.clear
|
154
|
+
expect(returned.object_id).to eq(id)
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
137
159
|
end
|
138
160
|
|
data/spec/caches/ttl_spec.rb
CHANGED
@@ -233,4 +233,26 @@ describe Caches::TTL do
|
|
233
233
|
|
234
234
|
end
|
235
235
|
|
236
|
+
describe "clear" do
|
237
|
+
|
238
|
+
it "clears all cached data" do
|
239
|
+
cache.clear
|
240
|
+
expect(cache.size).to eq(0)
|
241
|
+
end
|
242
|
+
|
243
|
+
it "clears the stats" do
|
244
|
+
cache[:a]
|
245
|
+
cache[:nope]
|
246
|
+
cache.clear
|
247
|
+
expect(cache.stats).to eq(hits: 0, misses: 0, hit_rate: "0.0%")
|
248
|
+
end
|
249
|
+
|
250
|
+
it "returns the empty cache" do
|
251
|
+
id = cache.object_id
|
252
|
+
returned = cache.clear
|
253
|
+
expect(returned.object_id).to eq(id)
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
236
258
|
end
|