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
14 changes: 14 additions & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This configuration file was generated by `ameba --gen-config`
# on 2026-03-11 00:06:56 UTC using Ameba version 1.6.4.
# The point is for the user to remove these configuration records
# one by one as the reported problems are removed from the code base.

# Problems found: 2
# Run `ameba --only Lint/UselessAssign` for details
Lint/UselessAssign:
Description: Disallows useless variable assignments
ExcludeTypeDeclarations: false
Excluded:
- src/lucky_cache.cr
Enabled: true
Severity: Warning
16 changes: 9 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
continue-on-error: false
steps:
- name: Download source
uses: actions/checkout@v3
uses: actions/checkout@v6
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
- name: Install shards
Expand All @@ -27,15 +27,17 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
crystal_version: [latest]
include:
- os: ubuntu-latest
crystal_version: 1.4.0
os:
- ubuntu-latest
- macos-latest
- windows-latest
crystal_version:
- 1.16.3
- latest
runs-on: ${{ matrix.os }}
continue-on-error: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v6
- uses: crystal-lang/install-crystal@v1
with:
crystal: ${{ matrix.crystal_version }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v6
with:
persist-credentials: false
- uses: crystal-lang/install-crystal@v1
Expand All @@ -17,7 +17,7 @@ jobs:
- name: "Generate docs"
run: crystal docs
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs
8 changes: 4 additions & 4 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 0.1.1
authors:
- Jeremy Woertink <jeremywoertink@gmail.com>

crystal: ">= 1.4.0"
crystal: ">= 1.16.0"

license: MIT

Expand All @@ -13,12 +13,12 @@ dependencies:
github: wyhaines/splay_tree_map.cr
habitat:
github: luckyframework/habitat
version: ~> 0.4.7
version: ~> 0.4.9

development_dependencies:
timecop:
github: crystal-community/timecop.cr
branch: master
version: ~> 0.5.0
ameba:
github: crystal-ameba/ameba
version: ~> 1.0.0
version: ~> 1.6.4
12 changes: 6 additions & 6 deletions spec/stores/memory_store_spec.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require "../spec_helper"

class User
include LuckyCache::Cachable
include LuckyCache::Cacheable
property email : String

def initialize(@email : String)
end
end

class Post
include LuckyCache::Cachable
include LuckyCache::Cacheable
property title : String

def initialize(@title : String)
Expand Down Expand Up @@ -49,7 +49,7 @@ describe LuckyCache::MemoryStore do

result.should be_a(Array(User))
counter.should eq(1)
friends = result.not_nil!
friends = result.as(Array(User))
friends.size.should eq(2)
friends.map(&.email).should contain("chris@email.net")
end
Expand All @@ -68,7 +68,7 @@ describe LuckyCache::MemoryStore do

result.should be_a(Array(Post))
counter.should eq(1)
friends = result.not_nil!
friends = result.as(Array(Post))
friends.size.should eq(2)
friends.map(&.title).should contain("learn about cash")
end
Expand All @@ -93,10 +93,10 @@ describe LuckyCache::MemoryStore do
UUID.random
end
Timecop.travel(12.hours.from_now) do
cache.read("coupon").not_nil!.expired?.should eq(false)
cache.read("coupon").as(LuckyCache::CacheItem).expired?.should eq(false)
end
Timecop.travel(35.hours.from_now) do
cache.read("coupon").not_nil!.expired?.should eq(false)
cache.read("coupon").as(LuckyCache::CacheItem).expired?.should eq(false)
end
Timecop.travel(49.hours.from_now) do
cache.read("coupon").should eq(nil)
Expand Down
8 changes: 4 additions & 4 deletions spec/stores/null_store_spec.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require "../spec_helper"

class User
include LuckyCache::Cachable
include LuckyCache::Cacheable
property email : String

def initialize(@email : String)
end
end

class Post
include LuckyCache::Cachable
include LuckyCache::Cacheable
property title : String

def initialize(@title : String)
Expand Down Expand Up @@ -49,7 +49,7 @@ describe LuckyCache::NullStore do

result.should be_a(Array(User))
counter.should eq(2)
friends = result.not_nil!
friends = result.as(Array(User))
friends.size.should eq(2)
friends.map(&.email).should contain("chris@email.net")
end
Expand All @@ -68,7 +68,7 @@ describe LuckyCache::NullStore do

result.should be_a(Array(Post))
counter.should eq(2)
friends = result.not_nil!
friends = result.as(Array(Post))
friends.size.should eq(2)
friends.map(&.title).should contain("learn about cash")
end
Expand Down
4 changes: 2 additions & 2 deletions src/lucky_cache.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "splay_tree_map"
require "habitat"
require "uuid"
require "json"
require "./lucky_cache/cachable"
require "./lucky_cache/cacheable"
require "./lucky_cache/cache_item"
require "./lucky_cache/stores/*"
require "./lucky_cache/*"
Expand All @@ -15,5 +15,5 @@ module LuckyCache
setting default_duration : Time::Span = 1.minute
end

alias CachableTypes = Cachable | Array(Cachable) | String | Array(String) | Int32 | Array(Int32) | Int64 | Array(Int64) | Float64 | Array(Float64) | Bool | Array(Bool) | Time | UUID | JSON::Any
alias CacheableTypes = Cacheable | Array(Cacheable) | String | Array(String) | Int32 | Array(Int32) | Int64 | Array(Int64) | Float64 | Array(Float64) | Bool | Array(Bool) | Time | UUID | JSON::Any
end
4 changes: 2 additions & 2 deletions src/lucky_cache/cache_item.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module LuckyCache
struct CacheItem
getter value : CachableTypes
getter value : CacheableTypes
getter expires_in : Time::Span
private getter expiration : Time

def initialize(@value : CachableTypes, @expires_in : Time::Span)
def initialize(@value : CacheableTypes, @expires_in : Time::Span)
@expiration = @expires_in.from_now
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Include this module in the custom types you want to cache
module LuckyCache::Cachable
module LuckyCache::Cacheable
end
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: 5 additions & 5 deletions src/lucky_cache/stores/memory_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module LuckyCache
data = yield

if data.is_a?(Array)
stored_data = [] of Cachable
data.each { |d| stored_data << d }
stored_data = [] of Cacheable
data.each { |datum| stored_data << datum }
else
stored_data = data
end
Expand All @@ -43,20 +43,20 @@ module LuckyCache
cache.clear
end

# If the `CacheItem` exists, it will map the `Array(Cachable)`
# If the `CacheItem` exists, it will map the `Array(Cacheable)`
# in to `Array(T)`. If no item is found, write the block value
# and return the block value
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) }
cache_item.value.as(Array(LuckyCache::Cacheable)).each { |v| new_array << v.as(T) }
new_array
else
write(key, expires_in: expires_in) { yield }
end
end

# If the `CacheItem` exists, it will cast the `Cachable`
# If the `CacheItem` exists, it will cast the `Cacheable`
# in to `T`. If no item is found, write the block value
# and return the block value
def fetch(key : CacheKey, *, as : T.class, expires_in : Time::Span = LuckyCache.settings.default_duration, &) forall T
Expand Down
Loading