persistent-cache 0.3.7 → 0.3.8
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.
- data/README.md +13 -0
- data/lib/persistent-cache.rb +10 -10
- data/lib/persistent-cache/version.rb +1 -1
- data/spec/persistent-cache_spec.rb +26 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -86,6 +86,19 @@ Or install it yourself as:
|
|
86
86
|
|
87
87
|
cache = Persistent::Cache.new("cache-name", nil, Persistent::Cache::STORAGE_RAM)
|
88
88
|
|
89
|
+
# Using .key?
|
90
|
+
cache = Persistent::Cache.new("cache-name", 1)
|
91
|
+
cache[1] = 2
|
92
|
+
cache[1]
|
93
|
+
# 2
|
94
|
+
sleep 2
|
95
|
+
cache.key?(1)
|
96
|
+
# 1
|
97
|
+
cache[1]
|
98
|
+
# nil
|
99
|
+
cache.key?(1)
|
100
|
+
# nil
|
101
|
+
|
89
102
|
## Encoding
|
90
103
|
|
91
104
|
Note that when using a back-end that requires marshalling (e.g. sqlite) the string encoding for []= and [] needs to be the same (e.g. UTF-8, US-ASCII, etc.) If the coding does not match, [] will not be able to find the entry during lookup and will return nil. See the section on 'Encoding' below for more detail.
|
data/lib/persistent-cache.rb
CHANGED
@@ -50,15 +50,6 @@ module Persistent
|
|
50
50
|
lookup_key(key)
|
51
51
|
end
|
52
52
|
|
53
|
-
def lookup_by_key_value(key)
|
54
|
-
if @storage.keys
|
55
|
-
@storage.keys.each do |k|
|
56
|
-
return lookup_key(k) if k == key
|
57
|
-
end
|
58
|
-
end
|
59
|
-
return nil
|
60
|
-
end
|
61
|
-
|
62
53
|
def each(&block)
|
63
54
|
keys.each do |key|
|
64
55
|
yield key, lookup_key(key)
|
@@ -84,6 +75,15 @@ module Persistent
|
|
84
75
|
Time.parse(result[1])
|
85
76
|
end
|
86
77
|
|
78
|
+
def key?(key)
|
79
|
+
if @storage.keys
|
80
|
+
@storage.keys.each do |k|
|
81
|
+
return k if k == key
|
82
|
+
end
|
83
|
+
end
|
84
|
+
return nil
|
85
|
+
end
|
86
|
+
|
87
87
|
private
|
88
88
|
|
89
89
|
def encode_if_requested(key)
|
@@ -110,7 +110,7 @@ module Persistent
|
|
110
110
|
return false if @fresh.nil?
|
111
111
|
|
112
112
|
timestamp = Time.parse(result[1])
|
113
|
-
if ((Time.now - timestamp) >
|
113
|
+
if ((Time.now - timestamp) > @fresh)
|
114
114
|
delete_entry(key)
|
115
115
|
return true
|
116
116
|
end
|
@@ -208,4 +208,30 @@ describe Persistent::Cache do
|
|
208
208
|
@pcache[@encoded_key] = "some value"
|
209
209
|
end
|
210
210
|
end
|
211
|
+
|
212
|
+
context "when needing to know if a key is in the cache" do
|
213
|
+
it "should return nil if the key is not present" do
|
214
|
+
setup_cache
|
215
|
+
expect(@pcache.key?(1)).to eql(nil)
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should return the key if the key is present" do
|
219
|
+
setup_cache
|
220
|
+
@pcache[1] = "1"
|
221
|
+
expect(@pcache.key?(1)).to eql(1)
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should return the key even if the entry is stale" do
|
225
|
+
setup_cache
|
226
|
+
@pcache[1] = "1"
|
227
|
+
sleep 2
|
228
|
+
expect(@pcache.key?(1)).to eql(1)
|
229
|
+
expect(@pcache[1]).to eql(nil)
|
230
|
+
end
|
231
|
+
|
232
|
+
def setup_cache(encoding = nil)
|
233
|
+
FileUtils.rm_f(@db_name)
|
234
|
+
@pcache = Persistent::Cache.new(@db_name, 1)
|
235
|
+
end
|
236
|
+
end
|
211
237
|
end
|