Skip to content

Commit ccbd33d

Browse files
authored
Merge pull request #11 from crystal-cache/cache-v2
Updated store implementation to use string keys only
2 parents a34eaed + 798e3ea commit ccbd33d

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require "redis_cache_store"
3030
It's important to note that Redis cache value must be string.
3131

3232
```crystal
33-
cache = Cache::RedisCacheStore(String, String).new(expires_in: 1.minute, namespace: "myapp-cache")
33+
cache = Cache::RedisCacheStore(String).new(expires_in: 1.minute, namespace: "myapp-cache")
3434
3535
# Fetches data from the Redis, using "myapp-cache:today" key. If there is data in
3636
# the Redis with the given key, then that data is returned.
@@ -65,7 +65,7 @@ If you need to connect to a remote server or a different port, try:
6565
```crystal
6666
redis_uri = URI.parse("rediss://:my-secret-pw@10.0.1.1:6380/1")
6767
redis = Redis::Client.new(uri: redis_uri)
68-
cache = Cache::RedisCacheStore(String, String).new(expires_in: 1.minute, cache: redis)
68+
cache = Cache::RedisCacheStore(String).new(expires_in: 1.minute, cache: redis)
6969
```
7070

7171
## Contributing

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors:
77
dependencies:
88
cache:
99
github: crystal-cache/cache
10-
version: ">= 0.15.0"
10+
version: ">= 1.0.0"
1111
redis:
1212
github: jgaskins/redis
1313

spec/redis_cache_store_spec.cr

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,62 @@ describe Cache do
99

1010
context "#initialize" do
1111
it "initialize" do
12-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours)
12+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours)
1313

14-
store.should be_a(Cache::RedisCacheStore(String, String))
14+
store.should be_a(Cache::RedisCacheStore(String))
1515
end
1616

1717
it "initialize with Redis::Client" do
1818
redis_uri = URI.parse("redis://localhost:6379/1")
1919
redis = Redis::Client.new(uri: redis_uri)
20-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours, cache: redis)
20+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours, cache: redis)
2121

22-
store.should be_a(Cache::RedisCacheStore(String, String))
22+
store.should be_a(Cache::RedisCacheStore(String))
2323
end
2424
end
2525

2626
context "instance methods" do
2727
it "#inspect without namescape" do
28-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours)
28+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours)
2929

3030
store.inspect.should match(
31-
/\A#<Cache\:\:RedisCacheStore\(String, String\) redis=#<Redis\:\:Client\:0x.*expires_in=12:00:00.*namespace=nil>\z/
31+
/\A#<Cache\:\:RedisCacheStore\(String\) redis=#<Redis\:\:Client\:0x.*expires_in=12:00:00.*namespace=nil>\z/
3232
)
3333
end
3434

3535
it "#inspect with namescape" do
36-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours, namespace: "myapp-cache")
36+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours, namespace: "myapp-cache")
3737

3838
store.inspect.should match(
39-
/\A#<Cache\:\:RedisCacheStore\(String, String\) redis=#<Redis\:\:Client\:0x.*expires_in=12:00:00.*namespace=\"myapp-cache\">\z/
39+
/\A#<Cache\:\:RedisCacheStore\(String\) redis=#<Redis\:\:Client\:0x.*expires_in=12:00:00.*namespace=\"myapp-cache\">\z/
4040
)
4141
end
4242

4343
it "#redis" do
44-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours)
44+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours)
4545
store.redis.should be_a(Redis::Client)
4646
end
4747

4848
it "#expires_in" do
49-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours)
49+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours)
5050
store.expires_in.should eq(12.hours)
5151
end
5252

5353
it "#namespace" do
54-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours, namespace: "myapp-cache")
54+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours, namespace: "myapp-cache")
5555
store.namespace.should eq("myapp-cache")
5656
end
5757
end
5858

5959
it "write to cache first time" do
60-
store = Cache::RedisCacheStore(String, String).new(12.hours)
60+
store = Cache::RedisCacheStore(String).new(12.hours)
6161

6262
value = store.fetch("foo") { "bar" }
6363
value.should eq("bar")
6464
end
6565

6666
it "fetch from cache" do
67-
store = Cache::RedisCacheStore(String, String).new(12.hours)
67+
store = Cache::RedisCacheStore(String).new(12.hours)
6868

6969
value = store.fetch("foo") { "bar" }
7070
value.should eq("bar")
@@ -76,7 +76,7 @@ describe Cache do
7676
it "fetch from cache with custom Redis::Client" do
7777
redis_uri = URI.parse("redis://localhost:6379/1")
7878
redis = Redis::Client.new(uri: redis_uri)
79-
store = Cache::RedisCacheStore(String, String).new(expires_in: 12.hours, cache: redis)
79+
store = Cache::RedisCacheStore(String).new(expires_in: 12.hours, cache: redis)
8080

8181
value = store.fetch("foo") { "bar" }
8282
value.should eq("bar")
@@ -86,7 +86,7 @@ describe Cache do
8686
end
8787

8888
it "don't fetch from cache if expired" do
89-
store = Cache::RedisCacheStore(String, String).new(1.seconds)
89+
store = Cache::RedisCacheStore(String).new(1.seconds)
9090

9191
value = store.fetch("foo") { "bar" }
9292
value.should eq("bar")
@@ -98,7 +98,7 @@ describe Cache do
9898
end
9999

100100
it "fetch with expires_in from cache" do
101-
store = Cache::RedisCacheStore(String, String).new(1.seconds)
101+
store = Cache::RedisCacheStore(String).new(1.seconds)
102102

103103
value = store.fetch("foo", expires_in: 1.hours) { "bar" }
104104
value.should eq("bar")
@@ -110,7 +110,7 @@ describe Cache do
110110
end
111111

112112
it "don't fetch with expires_in from cache if expires" do
113-
store = Cache::RedisCacheStore(String, String).new(12.hours)
113+
store = Cache::RedisCacheStore(String).new(12.hours)
114114

115115
value = store.fetch("foo", expires_in: 1.seconds) { "bar" }
116116
value.should eq("bar")
@@ -122,15 +122,15 @@ describe Cache do
122122
end
123123

124124
it "write" do
125-
store = Cache::RedisCacheStore(String, String).new(12.hours)
125+
store = Cache::RedisCacheStore(String).new(12.hours)
126126
store.write("foo", "bar", expires_in: 1.minute)
127127

128128
value = store.fetch("foo") { "bar" }
129129
value.should eq("bar")
130130
end
131131

132132
it "rewrite value" do
133-
store = Cache::RedisCacheStore(String, String).new(12.hours)
133+
store = Cache::RedisCacheStore(String).new(12.hours)
134134
store.write("foo", "bar", expires_in: 1.minute)
135135
store.write("foo", "baz", expires_in: 1.minute)
136136

@@ -139,15 +139,15 @@ describe Cache do
139139
end
140140

141141
it "read" do
142-
store = Cache::RedisCacheStore(String, String).new(12.hours)
142+
store = Cache::RedisCacheStore(String).new(12.hours)
143143
store.write("foo", "bar")
144144

145145
value = store.read("foo")
146146
value.should eq("bar")
147147
end
148148

149149
it "set a custom expires_in value for entry on write" do
150-
store = Cache::RedisCacheStore(String, String).new(12.hours)
150+
store = Cache::RedisCacheStore(String).new(12.hours)
151151
store.write("foo", "bar", expires_in: 1.second)
152152

153153
store.keys.should eq(Set{"foo"})
@@ -161,7 +161,7 @@ describe Cache do
161161
end
162162

163163
it "delete from cache" do
164-
store = Cache::RedisCacheStore(String, String).new(12.hours)
164+
store = Cache::RedisCacheStore(String).new(12.hours)
165165

166166
value = store.fetch("foo") { "bar" }
167167
value.should eq("bar")
@@ -175,7 +175,7 @@ describe Cache do
175175
end
176176

177177
it "deletes all items from the cache" do
178-
store = Cache::RedisCacheStore(String, String).new(12.hours)
178+
store = Cache::RedisCacheStore(String).new(12.hours)
179179

180180
value = store.fetch("foo") { "bar" }
181181
value.should eq("bar")
@@ -188,7 +188,7 @@ describe Cache do
188188
end
189189

190190
it "#exists?" do
191-
store = Cache::RedisCacheStore(String, String).new(12.hours)
191+
store = Cache::RedisCacheStore(String).new(12.hours)
192192

193193
store.write("foo", "bar")
194194

@@ -197,7 +197,7 @@ describe Cache do
197197
end
198198

199199
it "#exists? expires" do
200-
store = Cache::RedisCacheStore(String, String).new(1.second)
200+
store = Cache::RedisCacheStore(String).new(1.second)
201201

202202
store.write("foo", "bar")
203203

@@ -207,7 +207,7 @@ describe Cache do
207207
end
208208

209209
it "#increment" do
210-
store = Cache::RedisCacheStore(String, Int32).new(12.hours)
210+
store = Cache::RedisCacheStore(Int32).new(12.hours)
211211

212212
store.write("num", 1)
213213
store.increment("num", 1)
@@ -218,7 +218,7 @@ describe Cache do
218218
end
219219

220220
it "#decrement" do
221-
store = Cache::RedisCacheStore(String, Int32).new(12.hours)
221+
store = Cache::RedisCacheStore(Int32).new(12.hours)
222222

223223
store.write("num", 2)
224224
store.decrement("num", 1)
@@ -229,7 +229,7 @@ describe Cache do
229229
end
230230

231231
it "#increment non-existent value" do
232-
store = Cache::RedisCacheStore(String, Int32).new(12.hours)
232+
store = Cache::RedisCacheStore(Int32).new(12.hours)
233233

234234
store.increment("undef_num", 1)
235235

@@ -239,7 +239,7 @@ describe Cache do
239239
end
240240

241241
it "clear" do
242-
store = Cache::RedisCacheStore(String, String).new(12.hours)
242+
store = Cache::RedisCacheStore(String).new(12.hours)
243243

244244
store.write("foo", "bar", expires_in: 1.minute)
245245

@@ -251,10 +251,10 @@ describe Cache do
251251

252252
context "with namespace" do
253253
it "write" do
254-
store1 = Cache::RedisCacheStore(String, String).new(12.hours, namespace: "myapp-cache")
254+
store1 = Cache::RedisCacheStore(String).new(12.hours, namespace: "myapp-cache")
255255
store1.write("foo1", "bar", expires_in: 1.minute)
256256

257-
store2 = Cache::RedisCacheStore(String, String).new(12.hours)
257+
store2 = Cache::RedisCacheStore(String).new(12.hours)
258258
store2.write("foo1", "baz", expires_in: 1.minute)
259259

260260
store1.keys.should eq(Set{"foo1"})
@@ -265,8 +265,8 @@ describe Cache do
265265
end
266266

267267
it "clear" do
268-
store = Cache::RedisCacheStore(String, String).new(12.hours, namespace: "myapp-cache")
269-
other_store = Cache::RedisCacheStore(String, String).new(12.hours, namespace: "other-cache")
268+
store = Cache::RedisCacheStore(String).new(12.hours, namespace: "myapp-cache")
269+
other_store = Cache::RedisCacheStore(String).new(12.hours, namespace: "other-cache")
270270

271271
1001.times do |i|
272272
store.write("#{i + 1}", "bar", expires_in: 1.minute)

src/redis_cache_store.cr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Cache
55
# A cache store implementation which stores data in Redis.
66
#
77
# ```
8-
# cache = Cache::RedisCacheStore(String, String).new(expires_in: 1.minute, namespace: "myapp-cache")
8+
# cache = Cache::RedisCacheStore(String).new(expires_in: 1.minute, namespace: "myapp-cache")
99
#
1010
# # Fetches data from the Redis, using "myapp-cache:today" key. If there is data in
1111
# # the REdis with the given key, then that data is returned.
@@ -27,9 +27,9 @@ module Cache
2727
# ```
2828
# redis_uri = URI.parse("rediss://:my-secret-pw@10.0.1.1:6380/1")
2929
# redis = Redis::Client.new(uri: redis_uri)
30-
# cache = Cache::RedisCacheStore(String, String).new(expires_in: 1.minute, cache: redis)
30+
# cache = Cache::RedisCacheStore(String).new(expires_in: 1.minute, cache: redis)
3131
# ```
32-
struct RedisCacheStore(K, V) < Store(K, V)
32+
struct RedisCacheStore(V) < Store(V)
3333
@cache : Redis::Client
3434

3535
# The maximum number of entries to receive per SCAN call.
@@ -43,33 +43,33 @@ module Cache
4343
# server is shared with other apps:
4444
#
4545
# ```
46-
# Cache::RedisCacheStore(String, String).new(expires_in: 1.minute, namespace: "myapp-cache")
46+
# Cache::RedisCacheStore(String).new(expires_in: 1.minute, namespace: "myapp-cache")
4747
# ```
4848
def initialize(@expires_in : Time::Span, @cache = Redis::Client.new, @namespace : String? = nil)
4949
end
5050

51-
def keys : Set(K)
51+
def keys : Set(String)
5252
pattern = namespace_key("*")
5353
if namespace = @namespace
54-
redis.keys(pattern).map(&.as(K).sub(namespace + ':', "")).to_set
54+
redis.keys(pattern).map(&.as(String).sub(namespace + ':', "")).to_set
5555
else
56-
redis.keys(pattern).map(&.as(K)).to_set
56+
redis.keys(pattern).map(&.as(String)).to_set
5757
end
5858
end
5959

60-
private def write_impl(key : K, value : V, *, expires_in = @expires_in)
60+
private def write_impl(key : String, value : V, *, expires_in = @expires_in)
6161
redis.set(key, value.to_s, ex: expires_in)
6262
end
6363

64-
private def read_impl(key : K)
64+
private def read_impl(key : String)
6565
redis.get(key)
6666
end
6767

68-
def delete_impl(key : K) : Bool
68+
def delete_impl(key : String) : Bool
6969
redis.del(key) == 1_i64
7070
end
7171

72-
def exists_impl(key : K) : Bool
72+
def exists_impl(key : String) : Bool
7373
redis.exists(key) == 1
7474
end
7575

@@ -86,7 +86,7 @@ module Cache
8686
# Increment a cached value. This method uses the Redis incr atomic operator.
8787
#
8888
# Calling it on a value not stored will initialize that value to zero.
89-
def increment(key : K, amount = 1)
89+
def increment(key : String, amount = 1)
9090
key = namespace_key(key)
9191

9292
redis.incrby(key, amount).tap do
@@ -97,15 +97,15 @@ module Cache
9797
# Decrement a cached value. This method uses the Redis decr atomic operator.
9898
#
9999
# Calling it on a value not stored will initialize that value to zero.
100-
def decrement(key : K, amount = 1)
100+
def decrement(key : String, amount = 1)
101101
key = namespace_key(key)
102102

103103
redis.decrby(key, amount).tap do
104104
write_key_expiry(key)
105105
end
106106
end
107107

108-
private def write_key_expiry(key : K)
108+
private def write_key_expiry(key : String)
109109
redis.expire(key, @expires_in.total_seconds.to_i)
110110
end
111111

0 commit comments

Comments
 (0)