lru_redux 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -9
- data/lib/lru_redux/cache.rb +6 -5
- data/lib/lru_redux/version.rb +1 -1
- data/test/cache_test.rb +2 -2
- 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: f230b3f99aa8b6685b92b965735ab60f21cdd173
|
4
|
+
data.tar.gz: 0e7c60bcfd0e91283c135eefa51f4374bb3392c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 498a398539d0ef6d0160f9ccdd6f150178701f332bd812298f942b3d68a17495e90f408771e498af245a2ff70222faea9871ed2346f45c3543a4da02b992489c
|
7
|
+
data.tar.gz: a4c98d69dcc3ed3884aded54d944f18c60c3ecce2a75e91de0894e052f59381e6d9082061a41de2d22dba9e29cc29564e6130eff500186ed20fd5b91e5623c0f
|
data/README.md
CHANGED
@@ -24,16 +24,24 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
require 'lru_redux'
|
27
|
+
|
27
28
|
# non thread safe
|
28
29
|
cache = LruRedux::Cache(100)
|
29
30
|
cache[:a] = "1"
|
31
|
+
cache[:b] = "2"
|
32
|
+
cache[:a] # a pushed to front
|
33
|
+
# "1"
|
34
|
+
|
30
35
|
cache.to_a
|
31
|
-
# [[:a,"1"]]
|
36
|
+
# [[:a,"1"],[:b,"2"]]
|
32
37
|
cache.delete(:a)
|
33
38
|
cache.each {|k,v| p "#{k} #{v}"}
|
34
39
|
# nothing
|
40
|
+
cache.max_size(200) # cache now stores 200 items
|
41
|
+
cache.clear # cache has no items
|
35
42
|
|
36
|
-
# for thread safe
|
43
|
+
# for thread safe access, all methods on cache
|
44
|
+
# are protected with a mutex
|
37
45
|
cache = LruRedux::ThreadSafeCache(100)
|
38
46
|
|
39
47
|
```
|
@@ -72,11 +80,6 @@ lru_redux thread safe 2.590000 0.000000 2.590000 ( 2.600422)
|
|
72
80
|
|
73
81
|
## Changlog
|
74
82
|
|
75
|
-
###version 0.0.
|
76
|
-
|
77
|
-
- Added .clear method
|
78
|
-
|
79
|
-
###version 0.0.3 - 23-April-2013
|
83
|
+
###version 0.0.4 - 23-April-2013
|
80
84
|
|
81
|
-
-
|
82
|
-
- Added implemented using arrays for extra perf
|
85
|
+
- Initial version
|
data/lib/lru_redux/cache.rb
CHANGED
@@ -13,8 +13,10 @@ class LruRedux::Cache
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def max_size=(size)
|
16
|
+
raise ArgumentError.new(:max_size) if @max_size < 1
|
16
17
|
@max_size = size
|
17
18
|
while pop_tail
|
19
|
+
# no op
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
@@ -32,8 +34,8 @@ class LruRedux::Cache
|
|
32
34
|
move_to_head(node)
|
33
35
|
node[2] = val
|
34
36
|
else
|
35
|
-
pop_tail
|
36
37
|
@data[key] = add_to_head(key,val)
|
38
|
+
pop_tail
|
37
39
|
end
|
38
40
|
val
|
39
41
|
end
|
@@ -59,9 +61,7 @@ class LruRedux::Cache
|
|
59
61
|
end
|
60
62
|
|
61
63
|
def delete(k)
|
62
|
-
node = @data
|
63
|
-
if node
|
64
|
-
@data.delete(k)
|
64
|
+
if node = @data.delete(k)
|
65
65
|
prev = node[0]
|
66
66
|
nex = node[3]
|
67
67
|
|
@@ -124,10 +124,11 @@ class LruRedux::Cache
|
|
124
124
|
end
|
125
125
|
|
126
126
|
def pop_tail
|
127
|
-
if @data.length
|
127
|
+
if @data.length > @max_size
|
128
128
|
@data.delete(@tail[1])
|
129
129
|
@tail = @tail[3]
|
130
130
|
@tail[0] = nil
|
131
|
+
true
|
131
132
|
end
|
132
133
|
end
|
133
134
|
|
data/lib/lru_redux/version.rb
CHANGED
data/test/cache_test.rb
CHANGED