cr-pantry
cr-pantry
A pluggable, thread-safe caching shard for Crystal (in-process LRU default) with TTL expiration, stale-while-revalidate (SWR), fluent builder API, and declarative @[Pantry::Cache] annotation-wrapped instance methods. Zero external dependencies.
Features
- Annotation-Based Declarative API: Include
Pantryand annotate instance methods with@[Pantry::Cache(...)]for zero-boilerplate caching, dynamic keys, and lifecycle hooks. - Fluent Builder API: Chain
.key(),.ttl(),.stale_while_revalidate()/.swr(),.adapter(),.on(:hit/:miss/:stale/:set), and.fetch { ... }/.work { ... }. - Direct Functional API: Call
Pantry.pantry(key, ttl: ..., swr: ...) { ... }directly. - Pluggable Storage Adapters: Ships with a thread-safe in-process
Pantry::MemoryAdapterwith optional LRUmax_size, plus a cleanPantry::Adapterinterface for Redis, Valkey, SQLite, or custom backends (with built-inJSON::Serializablesupport viaSerializedRecord). - First-Class Crystal Durations: Native support for
Time::Span(1.minute,5.minutes,20.milliseconds), duration strings ("20ms","30s","1m","1h","1d"), or numeric milliseconds. - Stale-While-Revalidate (SWR): Serves stale entries immediately while coalescing background fiber revalidation so concurrent stale hits only trigger a single upstream refresh.
Installation
Add it to your shard.yml:
dependencies:
cr-pantry:
github: Gwash3189/cr-pantry
version: ~> 0.1.0
Then run:
shards install
And require it:
require "cr-pantry"
Usage
1. Annotation-Based Instance Method Wrapper
Include Pantry in your class and annotate any instance method with @[Pantry::Cache(...)]:
class DashboardService
include Pantry
@[Pantry::Cache(
key: "dashboard:stats",
ttl: 1.minute,
stale_while_revalidate: 5.minutes,
on: {hit: :on_hit, miss: :on_miss, stale: :on_stale},
)]
def stats : String
compute_heavy_stats
end
# Dynamic keys with argument interpolation or procs
@[Pantry::Cache(key: "user:#{id}", ttl: 30.seconds)]
def find_user(id : Int32) : String
fetch_user_from_db(id)
end
def on_hit(val : String, key : String)
# called on fresh cache hit (0, 1, or 2 args supported)
end
def on_miss(key : String)
# called on cache miss (0 or 1 arg supported)
end
def on_stale(val : String, key : String)
# called when serving stale value during SWR window
end
end
2. Fluent Builder API
# Untyped builder (infers return type from the fetch block)
stats = Pantry.key("dashboard:stats")
.ttl(1.minute) # or "1m", 60_000
.stale_while_revalidate(5.minutes)
.on(:hit) { |key| puts "hit #{key}" }
.on(:miss) { |key| puts "miss #{key}" }
.on(:stale) { |key| puts "stale #{key}" }
.fetch { compute_heavy_stats }
# Typed builder (passes strongly-typed value + key to handlers)
stats = Pantry.key("dashboard:stats", String)
.ttl(1.minute)
.swr(5.minutes)
.on_hit { |val, key| puts "hit #{key}: #{val}" }
.on_miss { |key| puts "miss #{key}" }
.on_stale { |val, key| puts "stale #{key}: #{val}" }
.on_set { |val, key| puts "set #{key}: #{val}" }
.fetch { compute_heavy_stats }
3. Direct Functional API
user = Pantry.pantry("user:#{id}", ttl: 30.seconds, swr: 2.minutes) do
db.users.find(id)
end
4. Direct Cache Manipulation
Pantry.key("session:1").ttl(1.hour).set({user_id: 123})
session = Pantry.key("session:1").get(NamedTuple(user_id: Int32))
exists = Pantry.has?("session:1")
Pantry.key("session:1").invalidate
Pantry.clear
5. Pluggable Adapters
By default, Pantry uses an in-process Pantry::MemoryAdapter. Configure an LRU max_size or plug in a custom Pantry::Adapter:
# Configure global in-memory adapter with LRU eviction at 1,000 items
Pantry.use(Pantry::MemoryAdapter.new(max_size: 1_000))
# Or use a custom adapter per operation:
data = Pantry.key("item:1")
.adapter(custom_adapter)
.ttl(10.minutes)
.fetch { load_item(1) }
Development
crystal spec --error-on-warnings
crystal tool format --check src spec
License
MIT License. Copyright (c) 2026 Adam Beck.
Repository
cr-pantry
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- about 2 hours ago
- November 29, 2026
License
MIT License
Links
Synced at
Sun, 29 Nov 2026 05:15:25 GMT
Languages