Skip to content

Commit 76a2de0

Browse files
committed
Prepare for 1.0.0
1 parent b4fc7a2 commit 76a2de0

7 files changed

Lines changed: 50 additions & 25 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<div align="center">
22
<h1>
33
ZenStack Cache
4-
<small>(beta)</small>
54
</h1>
65

76
Reduce response times and database load with query-level caching integrated with the ZenStack ORM.
@@ -27,9 +26,9 @@
2726
</div>
2827

2928
## Features
30-
* 🌐 **Redis Caching:** Centralizes your caching to scale across different systems.
31-
* 🖥️ **Memory Caching:** Simplifies caching when scale is not a concern.
32-
* 🛟 **Type-safe:** The caching options appear in the intellisense for all read queries.
29+
* 🌐 **Redis Cache:** A central cache to scale across different systems.
30+
* 🖥️ **Memory Cache:** A simple cache when scale is not a concern.
31+
* 🛟 **Type-safety:** The caching options appear in the intellisense for all read queries.
3332

3433
## Requirements
3534

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"packages/*"
3131
],
3232
"catalog": {
33-
"@zenstackhq/orm": "canary",
33+
"@zenstackhq/orm": "^3.3.0",
3434
"zod": "^4.1.0",
3535
"kysely": "~0.28.8",
3636
"ioredis": "^5.0.0",

packages/cache/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@visualbravo/zenstack-cache",
33
"version": "0.0.0",
4-
"type": "module",
54
"license": "MIT",
5+
"repository": "github:visualbravo/zenstack-cache",
6+
"type": "module",
67
"main": "./dist/index.cjs",
78
"module": "./dist/index.mjs",
89
"types": "./dist/index.d.cts",
9-
"repository": "github:visualbravo/zenstack-cache",
1010
"exports": {
1111
".": {
1212
"@zenstack-cache/source": "./src/index.ts",
@@ -105,10 +105,10 @@
105105
"zod": "catalog:"
106106
},
107107
"devDependencies": {
108+
"@types/better-sqlite3": "7.6.13",
108109
"@zenstack-cache/config": "workspace:*",
109-
"kysely": "catalog:",
110110
"better-sqlite3": "12.6.2",
111-
"@types/better-sqlite3": "7.6.13"
111+
"kysely": "catalog:"
112112
},
113113
"peerDependencies": {
114114
"@zenstackhq/orm": "catalog:"

packages/cache/src/providers/redis.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class RedisCacheProvider implements CacheProvider {
1111
}
1212

1313
async get(key: string) {
14-
const entryJson = await this.redis.get(formatQueryKey(key))
14+
const entryJson = await this.redis.get(makeQueryKey(key))
1515

1616
if (!entryJson) {
1717
return undefined
@@ -22,28 +22,27 @@ export class RedisCacheProvider implements CacheProvider {
2222

2323
async set(key: string, entry: CacheEntry) {
2424
const multi = this.redis.multi()
25-
const formattedKey = formatQueryKey(key)
25+
const queryKey = makeQueryKey(key)
2626

27-
multi.set(formattedKey, superjson.stringify(entry))
27+
multi.set(queryKey, superjson.stringify(entry))
2828

2929
const totalTtl = getTotalTtl(entry)
3030

3131
if (totalTtl > 0) {
32-
multi.expire(formattedKey, totalTtl)
32+
multi.expire(queryKey, totalTtl)
3333
}
3434

3535
if (entry.options.tags) {
3636
for (const tag of entry.options.tags) {
37-
const formattedTagKey = formatTagKey(tag)
37+
const tagKey = makeTagKey(tag)
3838

39-
multi.sadd(formattedTagKey, formattedKey)
39+
multi.sadd(tagKey, queryKey)
4040

4141
if (totalTtl > 0) {
42-
multi.expire(formattedTagKey, totalTtl, 'GT')
43-
multi.expire(formattedTagKey, totalTtl, 'NX')
44-
}
45-
else {
46-
multi.persist(formattedTagKey)
42+
multi.expire(tagKey, totalTtl, 'GT')
43+
multi.expire(tagKey, totalTtl, 'NX')
44+
} else {
45+
multi.persist(tagKey)
4746
}
4847
}
4948
}
@@ -56,7 +55,7 @@ export class RedisCacheProvider implements CacheProvider {
5655
await Promise.all(
5756
options.tags.map(tag => {
5857
return new Promise((resolve, reject) => {
59-
const stream = this.redis.sscanStream(formatTagKey(tag), {
58+
const stream = this.redis.sscanStream(makeTagKey(tag), {
6059
count: 100,
6160
})
6261

@@ -97,10 +96,10 @@ export type RedisCacheProviderOptions = {
9796
url: string
9897
}
9998

100-
function formatQueryKey(key: string) {
99+
function makeQueryKey(key: string) {
101100
return `zenstack:cache:query:${key}`
102101
}
103102

104-
function formatTagKey(key: string) {
103+
function makeTagKey(key: string) {
105104
return `zenstack:cache:tag:${key}`
106105
}

packages/cache/src/superjson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ superjson.registerCustom<Decimal, string>(
1010
'decimal.js',
1111
)
1212

13-
export { superjson }
13+
export { superjson }

packages/cache/tests/memory.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ describe('Cache plugin (memory)', () => {
362362
},
363363
})
364364

365-
vi.advanceTimersByTime(60000)
365+
vi.advanceTimersByTime(70000)
366366

367367
await expect(
368368
extDb.user.exists({
@@ -1052,6 +1052,24 @@ describe('Cache plugin (memory)', () => {
10521052
}),
10531053
)
10541054

1055+
await extDb.user.findMany({
1056+
cache: {},
1057+
})
1058+
1059+
await extDb.user.create({
1060+
data: {
1061+
email: 'test@email.com',
1062+
},
1063+
})
1064+
1065+
vi.advanceTimersByTime(70000)
1066+
1067+
await expect(
1068+
extDb.user.findMany({
1069+
cache: {},
1070+
}),
1071+
).resolves.toHaveLength(0)
1072+
10551073
await expect(
10561074
extDb.user.findMany({
10571075
cache: {

packages/cache/tests/redis.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Redis, Pipeline } from 'ioredis'
99

1010
const expire = vi.spyOn(Pipeline.prototype, 'expire')
1111
const sadd = vi.spyOn(Pipeline.prototype, 'sadd')
12+
const set = vi.spyOn(Pipeline.prototype, 'set')
1213

1314
describe('Cache plugin (redis)', () => {
1415
let db: ClientContract<typeof schema>
@@ -25,6 +26,7 @@ describe('Cache plugin (redis)', () => {
2526

2627
expire.mockClear()
2728
sadd.mockClear()
29+
set.mockClear()
2830

2931
await db.$pushSchema()
3032
await redis.flushdb()
@@ -1014,6 +1016,13 @@ describe('Cache plugin (redis)', () => {
10141016
}),
10151017
)
10161018

1019+
await extDb.user.findMany({
1020+
cache: {},
1021+
})
1022+
1023+
expect(set).toHaveBeenCalled()
1024+
expect(expire).not.toHaveBeenCalled()
1025+
10171026
await expect(
10181027
extDb.user.findMany({
10191028
cache: {

0 commit comments

Comments
 (0)