Skip to content

Commit c90dbd8

Browse files
authored
Code and specs form lucky cache PR + github CI (#1)
1 parent b831a5c commit c90dbd8

File tree

9 files changed

+643
-20
lines changed

9 files changed

+643
-20
lines changed

.github/workflows/ci.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: LuckyCache Redis Store CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: "*"
8+
9+
jobs:
10+
check_format:
11+
strategy:
12+
fail-fast: false
13+
runs-on: ubuntu-latest
14+
continue-on-error: false
15+
steps:
16+
- name: Download source
17+
uses: actions/checkout@v3
18+
- name: Install Crystal
19+
uses: crystal-lang/install-crystal@v1
20+
- name: Install shards
21+
run: shards install
22+
- name: Format
23+
run: crystal tool format --check
24+
- name: Lint
25+
run: ./bin/ameba
26+
specs:
27+
strategy:
28+
fail-fast: false
29+
matrix:
30+
os: [ubuntu-latest, windows-latest]
31+
crystal_version: [latest]
32+
include:
33+
- os: ubuntu-latest
34+
crystal_version: 1.4.0
35+
runs-on: ${{ matrix.os }}
36+
continue-on-error: false
37+
services:
38+
redis:
39+
image: redis:7-alpine
40+
options: >-
41+
--health-cmd "redis-cli ping"
42+
--health-interval 10s
43+
--health-timeout 5s
44+
--health-retries 5
45+
ports:
46+
- 6379:6379
47+
steps:
48+
- uses: actions/checkout@v3
49+
- uses: crystal-lang/install-crystal@v1
50+
with:
51+
crystal: ${{ matrix.crystal_version }}
52+
- name: Install dependencies
53+
run: shards install --skip-postinstall --skip-executables
54+
- name: Run tests
55+
run: crystal spec

.github/workflows/docs.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Deploy docs
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
with:
13+
persist-credentials: false
14+
- uses: crystal-lang/install-crystal@v1
15+
- name: "Install shards"
16+
run: shards install
17+
- name: "Generate docs"
18+
run: crystal docs
19+
- name: Deploy to GitHub Pages
20+
uses: peaceiris/actions-gh-pages@v3
21+
with:
22+
github_token: ${{ secrets.GITHUB_TOKEN }}
23+
publish_dir: ./docs

README.md

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# LuckyCacheRedisStore
1+
# LuckyCache Redis Store
22

3-
An adapter for [LuckyCache](https://github.com/luckyframework/lucky_cache/) to store
4-
data in Redis.
3+
A Redis storage backend for [LuckyCache](https://github.com/luckyframework/lucky_cache/), providing distributed caching capabilities for Lucky Framework applications.
54

65
## Installation
76

@@ -20,14 +19,84 @@ data in Redis.
2019
## Usage
2120

2221
```crystal
22+
require "lucky_cache"
2323
require "lucky_cache_redis_store"
24+
require "redis"
25+
26+
LuckyCache.configure do |settings|
27+
settings.storage = LuckyCache::RedisStore.new(
28+
Redis::Client.new(host: "localhost", port: 6379),
29+
prefix: "myapp:cache:"
30+
)
31+
settings.default_duration = 5.minutes
32+
end
2433
```
2534

26-
TODO: Write usage instructions here
35+
### Basic Usage
36+
37+
```crystal
38+
cache = LuckyCache.settings.storage
39+
40+
# Write to cache
41+
cache.write("my_key", expires_in: 1.hour) { "my value" }
42+
43+
# Read from cache
44+
if item = cache.read("my_key")
45+
puts item.value # => "my value"
46+
end
47+
48+
# Fetch (read-through cache)
49+
value = cache.fetch("computed_key", as: String, expires_in: 10.minutes) do
50+
# This block is only executed if the key doesn't exist
51+
expensive_computation
52+
end
53+
54+
# Delete from cache
55+
cache.delete("my_key")
56+
57+
# Clear all cached items with the configured prefix
58+
cache.flush
59+
```
60+
61+
### Supported Types
62+
63+
The Redis store supports the following types:
64+
- Basic types: `String`, `Int32`, `Int64`, `Float64`, `Bool`, `Time`, `UUID`, `JSON::Any`
65+
- Arrays of basic types: `Array(String)`, `Array(Int32)`, `Array(Int64)`, `Array(Float64)`, `Array(Bool)`
66+
67+
**Note:** Custom objects that include `LuckyCache::Cachable` are not supported by RedisStore due to serialization limitations. Use MemoryStore for caching custom objects.
68+
69+
### Workaround for Custom Objects
70+
71+
You can cache JSON representations of your objects:
72+
73+
```crystal
74+
# Instead of caching the object directly
75+
# cache.write("user:123") { User.new("test@example.com") } # This will raise an error
76+
77+
# Cache a JSON representation
78+
user_data = {"id" => 123, "email" => "test@example.com"}
79+
cache.write("user:123") { JSON::Any.new(user_data) }
80+
81+
# Retrieve and reconstruct
82+
cached_data = cache.read("user:123").not_nil!.value.as(JSON::Any)
83+
user = User.new(cached_data["email"].as_s)
84+
```
2785

2886
## Development
2987

30-
TODO: Write development instructions here
88+
To run the tests:
89+
90+
1. Make sure Redis is running locally on the default port (6379)
91+
2. Run `crystal spec`
92+
93+
The test suite includes tests for:
94+
- Basic type caching
95+
- Array type caching
96+
- Expiration functionality
97+
- Key deletion and cache flushing
98+
- Custom prefix support
99+
- Error handling for non-serializable types
31100

32101
## Contributing
33102

@@ -39,4 +108,4 @@ TODO: Write development instructions here
39108

40109
## Contributors
41110

42-
- [your-name-here](https://github.com/your-github-user) - creator and maintainer
111+
- [Jeremy Woertink](https://github.com/jwoertink) - creator and maintainer

shard.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ crystal: '>= 1.14.1'
88

99
license: MIT
1010

11+
dependencies:
12+
redis:
13+
github: jgaskins/redis
14+
lucky_cache:
15+
github: luckyframework/lucky_cache
16+
1117
development_dependencies:
1218
ameba:
1319
github: crystal-ameba/ameba
1420
version: ~> 1.5.0
15-
lucky_cache:
16-
github: luckyframework/lucky_cache
21+
timecop:
22+
github: crystal-community/timecop.cr

0 commit comments

Comments
 (0)