Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions spec/stores/memory_store_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,92 @@ describe LuckyCache::MemoryStore do
friends.map(&.title).should contain("learn about cash")
end

it "caches an array of strings" do
cache = LuckyCache::MemoryStore.new
counter = 0
cache.fetch("tags", as: Array(String)) do
counter += 1
["foo", "bar", "baz"]
end
result = cache.fetch("tags", as: Array(String)) do
counter += 1
["foo", "bar", "baz"]
end

result.should be_a(Array(String))
counter.should eq(1)
result.size.should eq(3)
result.should eq(["foo", "bar", "baz"])
end

it "caches an array of Int32" do
cache = LuckyCache::MemoryStore.new
counter = 0
cache.fetch("numbers", as: Array(Int32)) do
counter += 1
[1, 2, 3]
end
result = cache.fetch("numbers", as: Array(Int32)) do
counter += 1
[1, 2, 3]
end

result.should be_a(Array(Int32))
counter.should eq(1)
result.should eq([1, 2, 3])
end

it "caches an array of Int64" do
cache = LuckyCache::MemoryStore.new
counter = 0
cache.fetch("big_numbers", as: Array(Int64)) do
counter += 1
[100_i64, 200_i64]
end
result = cache.fetch("big_numbers", as: Array(Int64)) do
counter += 1
[100_i64, 200_i64]
end

result.should be_a(Array(Int64))
counter.should eq(1)
result.should eq([100_i64, 200_i64])
end

it "caches an array of Float64" do
cache = LuckyCache::MemoryStore.new
counter = 0
cache.fetch("scores", as: Array(Float64)) do
counter += 1
[1.5, 2.7, 3.9]
end
result = cache.fetch("scores", as: Array(Float64)) do
counter += 1
[1.5, 2.7, 3.9]
end

result.should be_a(Array(Float64))
counter.should eq(1)
result.should eq([1.5, 2.7, 3.9])
end

it "caches an array of Bool" do
cache = LuckyCache::MemoryStore.new
counter = 0
cache.fetch("flags", as: Array(Bool)) do
counter += 1
[true, false, true]
end
result = cache.fetch("flags", as: Array(Bool)) do
counter += 1
[true, false, true]
end

result.should be_a(Array(Bool))
counter.should eq(1)
result.should eq([true, false, true])
end

it "caches basic types" do
cache = LuckyCache::MemoryStore.new
str = cache.fetch("string:key", as: String) { "test" }
Expand Down
12 changes: 10 additions & 2 deletions src/lucky_cache/stores/memory_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ module LuckyCache
def write(key : CacheKey, *, expires_in : Time::Span = LuckyCache.settings.default_duration, &)
data = yield

if data.is_a?(Array)
# This is because types like `String`, `Int32`, etc... don't include `Cacheable` like a custom type would.
# In order to support these, we have to account for them separately.
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))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this... I'd rather see something like CachableType or whatever, but Crystal gets a little weird with type aliases and such. This is fine enough for now

stored_data = data
elsif data.is_a?(Array)
stored_data = [] of Cacheable
data.each { |datum| stored_data << datum }
else
Expand Down Expand Up @@ -49,7 +53,11 @@ module LuckyCache
def fetch(key : CacheKey, *, as : Array(T).class, expires_in : Time::Span = LuckyCache.settings.default_duration, &) forall T
if cache_item = read(key)
new_array = Array(T).new
cache_item.value.as(Array(LuckyCache::Cacheable)).each { |v| new_array << v.as(T) }
{% if T < LuckyCache::Cacheable %}
cache_item.value.as(Array(LuckyCache::Cacheable)).each { |val| new_array << val.as(T) }
{% else %}
cache_item.value.as(Array(T)).each { |val| new_array << val }
{% end %}
new_array
else
write(key, expires_in: expires_in) { yield }
Expand Down
Loading