Skip to content

Commit 04e668a

Browse files
authored
Updating MemoryStore so it can support arrays. (#17)
* Updating MemoryStore so it can support arrays. Fixes #15 * Adding a small comment to describe why the array handling is different
1 parent f8a4895 commit 04e668a

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

spec/stores/memory_store_spec.cr

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,92 @@ describe LuckyCache::MemoryStore do
7373
friends.map(&.title).should contain("learn about cash")
7474
end
7575

76+
it "caches an array of strings" do
77+
cache = LuckyCache::MemoryStore.new
78+
counter = 0
79+
cache.fetch("tags", as: Array(String)) do
80+
counter += 1
81+
["foo", "bar", "baz"]
82+
end
83+
result = cache.fetch("tags", as: Array(String)) do
84+
counter += 1
85+
["foo", "bar", "baz"]
86+
end
87+
88+
result.should be_a(Array(String))
89+
counter.should eq(1)
90+
result.size.should eq(3)
91+
result.should eq(["foo", "bar", "baz"])
92+
end
93+
94+
it "caches an array of Int32" do
95+
cache = LuckyCache::MemoryStore.new
96+
counter = 0
97+
cache.fetch("numbers", as: Array(Int32)) do
98+
counter += 1
99+
[1, 2, 3]
100+
end
101+
result = cache.fetch("numbers", as: Array(Int32)) do
102+
counter += 1
103+
[1, 2, 3]
104+
end
105+
106+
result.should be_a(Array(Int32))
107+
counter.should eq(1)
108+
result.should eq([1, 2, 3])
109+
end
110+
111+
it "caches an array of Int64" do
112+
cache = LuckyCache::MemoryStore.new
113+
counter = 0
114+
cache.fetch("big_numbers", as: Array(Int64)) do
115+
counter += 1
116+
[100_i64, 200_i64]
117+
end
118+
result = cache.fetch("big_numbers", as: Array(Int64)) do
119+
counter += 1
120+
[100_i64, 200_i64]
121+
end
122+
123+
result.should be_a(Array(Int64))
124+
counter.should eq(1)
125+
result.should eq([100_i64, 200_i64])
126+
end
127+
128+
it "caches an array of Float64" do
129+
cache = LuckyCache::MemoryStore.new
130+
counter = 0
131+
cache.fetch("scores", as: Array(Float64)) do
132+
counter += 1
133+
[1.5, 2.7, 3.9]
134+
end
135+
result = cache.fetch("scores", as: Array(Float64)) do
136+
counter += 1
137+
[1.5, 2.7, 3.9]
138+
end
139+
140+
result.should be_a(Array(Float64))
141+
counter.should eq(1)
142+
result.should eq([1.5, 2.7, 3.9])
143+
end
144+
145+
it "caches an array of Bool" do
146+
cache = LuckyCache::MemoryStore.new
147+
counter = 0
148+
cache.fetch("flags", as: Array(Bool)) do
149+
counter += 1
150+
[true, false, true]
151+
end
152+
result = cache.fetch("flags", as: Array(Bool)) do
153+
counter += 1
154+
[true, false, true]
155+
end
156+
157+
result.should be_a(Array(Bool))
158+
counter.should eq(1)
159+
result.should eq([true, false, true])
160+
end
161+
76162
it "caches basic types" do
77163
cache = LuckyCache::MemoryStore.new
78164
str = cache.fetch("string:key", as: String) { "test" }

src/lucky_cache/stores/memory_store.cr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ module LuckyCache
1818
def write(key : CacheKey, *, expires_in : Time::Span = LuckyCache.settings.default_duration, &)
1919
data = yield
2020

21-
if data.is_a?(Array)
21+
# This is because types like `String`, `Int32`, etc... don't include `Cacheable` like a custom type would.
22+
# In order to support these, we have to account for them separately.
23+
if data.is_a?(Array(String)) || data.is_a?(Array(Int32)) || data.is_a?(Array(Int64)) || data.is_a?(Array(Float64)) || data.is_a?(Array(Bool))
24+
stored_data = data
25+
elsif data.is_a?(Array)
2226
stored_data = [] of Cacheable
2327
data.each { |datum| stored_data << datum }
2428
else
@@ -49,7 +53,11 @@ module LuckyCache
4953
def fetch(key : CacheKey, *, as : Array(T).class, expires_in : Time::Span = LuckyCache.settings.default_duration, &) forall T
5054
if cache_item = read(key)
5155
new_array = Array(T).new
52-
cache_item.value.as(Array(LuckyCache::Cacheable)).each { |v| new_array << v.as(T) }
56+
{% if T < LuckyCache::Cacheable %}
57+
cache_item.value.as(Array(LuckyCache::Cacheable)).each { |val| new_array << val.as(T) }
58+
{% else %}
59+
cache_item.value.as(Array(T)).each { |val| new_array << val }
60+
{% end %}
5361
new_array
5462
else
5563
write(key, expires_in: expires_in) { yield }

0 commit comments

Comments
 (0)