timedcache 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/doc/created.rid +1 -1
- data/lib/timedcache.rb +8 -8
- data/specs/timed_cache_spec.rb +14 -1
- metadata +2 -2
data/doc/created.rid
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Mon Mar 26 22:35:48 +0100 2007
|
data/lib/timedcache.rb
CHANGED
@@ -41,7 +41,7 @@ require "pstore"
|
|
41
41
|
#
|
42
42
|
# Note that objects that cannot be marshalled (e.g. a Proc) can't be stored using the file-based cache.
|
43
43
|
class TimedCache
|
44
|
-
Version = "0.1"
|
44
|
+
Version = "0.1.1"
|
45
45
|
|
46
46
|
attr_reader :default_timeout
|
47
47
|
|
@@ -107,17 +107,17 @@ class TimedCache
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def put(key, value, timeout)
|
110
|
-
@cache[key
|
110
|
+
@cache[key] = ObjectContainer.new(value, timeout)
|
111
111
|
# Return just the given value, so that references to the
|
112
112
|
# ObjectStore instance can't be held outside this TimedCache:
|
113
113
|
value
|
114
114
|
end
|
115
115
|
|
116
116
|
def get(key)
|
117
|
-
if object_store = @cache[key
|
117
|
+
if object_store = @cache[key]
|
118
118
|
if object_store.expired?
|
119
119
|
# Free up memory:
|
120
|
-
@cache[key
|
120
|
+
@cache[key] = nil
|
121
121
|
else
|
122
122
|
object_store.object
|
123
123
|
end
|
@@ -137,7 +137,7 @@ class TimedCache
|
|
137
137
|
|
138
138
|
def put(key, value, timeout = nil)
|
139
139
|
@cache.transaction do
|
140
|
-
@cache[key
|
140
|
+
@cache[key] = ObjectContainer.new(value, timeout)
|
141
141
|
end
|
142
142
|
|
143
143
|
# Return just the given value, so that references to the
|
@@ -147,16 +147,16 @@ class TimedCache
|
|
147
147
|
|
148
148
|
def get(key)
|
149
149
|
@cache.transaction do
|
150
|
-
if object_store = @cache[key
|
150
|
+
if object_store = @cache[key]
|
151
151
|
if object_store.expired?
|
152
152
|
# Free up memory:
|
153
|
-
@cache[key
|
153
|
+
@cache[key] = nil
|
154
154
|
else
|
155
155
|
object_store.object
|
156
156
|
end
|
157
157
|
end
|
158
158
|
end
|
159
|
-
end
|
159
|
+
end
|
160
160
|
end
|
161
161
|
|
162
162
|
class ObjectContainer #:nodoc:
|
data/specs/timed_cache_spec.rb
CHANGED
@@ -28,6 +28,19 @@ context "Adding and retrieving objects from the cache" do
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
specify "String and symbol keys are not treated as equivalent" do
|
32
|
+
@caches.each do |cache|
|
33
|
+
cache[:symbolkey] = "Referenced by symbol"
|
34
|
+
cache["stringkey"] = "Referenced by string"
|
35
|
+
|
36
|
+
cache[:symbolkey].should_equal "Referenced by symbol"
|
37
|
+
cache["symbolkey"].should_equal nil
|
38
|
+
|
39
|
+
cache["stringkey"].should_equal "Referenced by string"
|
40
|
+
cache[:stringkey].should_equal nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
31
44
|
specify "After the specified timeout value has elapsed, nil should be returned" do
|
32
45
|
@caches.each do |cache|
|
33
46
|
cache.put(:myobject, "This needs caching", 0).should_equal "This needs caching"
|
@@ -36,7 +49,7 @@ context "Adding and retrieving objects from the cache" do
|
|
36
49
|
end
|
37
50
|
|
38
51
|
specify "If no object matching the given key is found, nil should be returned" do
|
39
|
-
@caches.each do |cache|
|
52
|
+
@caches.each do |cache|
|
40
53
|
cache.get(:my_nonexistant_object).should_equal nil
|
41
54
|
end
|
42
55
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: timedcache
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version:
|
7
|
-
date:
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2007-03-26 00:00:00 +01:00
|
8
8
|
summary: A very simple time-based object cache.
|
9
9
|
require_paths:
|
10
10
|
- lib
|