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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af4d80362e99b12a12ab32c6a8274f216195d666
4
- data.tar.gz: 1d68495be201eadc3c851b4efdfb6efb7128e710
3
+ metadata.gz: f230b3f99aa8b6685b92b965735ab60f21cdd173
4
+ data.tar.gz: 0e7c60bcfd0e91283c135eefa51f4374bb3392c1
5
5
  SHA512:
6
- metadata.gz: a836ef1195b172d1d3bf9f4091e8fbfd5802546ef45504f75d0db2842d1943a83df9df83b0138eb199deea1e52449bb7ef2613a8bfb386b5e81c8d42df7a7d5a
7
- data.tar.gz: 25cce1c929ad8eda1975d3af356b9705f114042f85d14a5b4aa7a128be89f12dc3cfe6b8a93837a692a9f7932efd6ff753592e216972760c1e20b61e4736b557
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.2 - 23-April-2013
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
- - Added .max_size= so you can resize caches on demand
82
- - Added implemented using arrays for extra perf
85
+ - Initial version
@@ -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[k]
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 >= @max_size
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
 
@@ -1,3 +1,3 @@
1
1
  module LruRedux
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -74,7 +74,7 @@ class CacheTest < MiniTest::Unit::TestCase
74
74
  @c[:a] = 1
75
75
  @c[:b] = 2
76
76
  @c[:c] = 3
77
- @c.max_size = 2
78
- assert_equal [[:c,3],[:b,2]], @c.to_a
77
+ @c.max_size = 1
78
+ assert_equal [[:c,3]], @c.to_a
79
79
  end
80
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lru_redux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron