Skip to content

Commit dcd5164

Browse files
committed
add keys
1 parent 40f9128 commit dcd5164

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

spec/redis_cache_store_spec.cr

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ describe Cache do
150150
store = Cache::RedisCacheStore(String, String).new(12.hours)
151151
store.write("foo", "bar", expires_in: 1.second)
152152

153+
store.keys.should eq(Set{"foo"})
154+
153155
sleep 2.seconds
154156

157+
store.keys.should be_empty
158+
155159
value = store.read("foo")
156160
value.should eq(nil)
157161
end
@@ -167,7 +171,7 @@ describe Cache do
167171

168172
value = store.read("foo")
169173
value.should eq(nil)
170-
store.keys.should eq(Set(String).new)
174+
store.keys.should be_empty
171175
end
172176

173177
it "deletes all items from the cache" do
@@ -247,10 +251,16 @@ describe Cache do
247251

248252
context "with namespace" do
249253
it "write" do
250-
store = Cache::RedisCacheStore(String, String).new(12.hours, namespace: "myapp-cache")
251-
store.write("foo", "bar", expires_in: 1.minute)
254+
store1 = Cache::RedisCacheStore(String, String).new(12.hours, namespace: "myapp-cache")
255+
store1.write("foo1", "bar", expires_in: 1.minute)
256+
257+
store2 = Cache::RedisCacheStore(String, String).new(12.hours)
258+
store2.write("foo2", "baz", expires_in: 1.minute)
259+
260+
store1.keys.should eq(Set{"myapp-cache:foo1"})
261+
store2.keys.should eq(Set{"myapp-cache:foo1", "foo2"})
252262

253-
value = store.fetch("foo") { "bar" }
263+
value = store1.fetch("foo") { "bar" }
254264
value.should eq("bar")
255265
end
256266

src/redis_cache_store.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ module Cache
4848
def initialize(@expires_in : Time::Span, @cache = Redis::Client.new, @namespace : String? = nil)
4949
end
5050

51+
def keys : Set(K)
52+
pattern = namespace_key("*")
53+
redis.keys(pattern).map(&.as(K)).to_set
54+
end
55+
5156
private def write_impl(key : K, value : V, *, expires_in = @expires_in)
5257
redis.set(key, value.to_s, ex: expires_in)
5358
end

0 commit comments

Comments
 (0)