Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/lucky_cache/html_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module LuckyCache::HtmlHelpers
# end
# end
# ```
def cache(key, *, expires_in : Time::Span?)
def cache(key, *, expires_in : Time::Span?, &)
cache = LuckyCache.settings.storage
expires = expires_in || LuckyCache.settings.default_duration

Expand Down
10 changes: 8 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,9 @@ module LuckyCache
def write(key : CacheKey, *, expires_in : Time::Span = LuckyCache.settings.default_duration, &)
data = yield

if data.is_a?(Array)
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
Copy Markdown
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 Cachable
data.each { |d| stored_data << d }
else
Expand Down Expand Up @@ -49,7 +51,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::Cachable)).each { |v| new_array << v.as(T) }
{% if T < LuckyCache::Cachable %}
cache_item.value.as(Array(LuckyCache::Cachable)).each { |v| new_array << v.as(T) }
{% else %}
cache_item.value.as(Array(T)).each { |v| new_array << v }
{% end %}
new_array
else
write(key, expires_in: expires_in) { yield }
Expand Down
Loading