Skip to content

Commit 1c4880c

Browse files
committed
Fix caching 0 as forever
1 parent 6f9bd94 commit 1c4880c

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
},
2020
"scripts": {
21-
"test": "tsx --test",
21+
"test": "tsx --test ./src/*.test.ts",
2222
"lint": "eslint . && prettier --check .",
2323
"build": "pkgroll --sourcemap",
2424
"prepare": "npm run build"

src/lru.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class LruMemoryCacheStore<T> implements CacheStore<T> {
6262

6363
set(key: string, record: StoreEntity<T>): Promise<void> {
6464
const ttl = record.expiresAt - Date.now();
65+
if (ttl <= 0) return Promise.resolve();
6566

6667
this.cache.set(key, record, { ttl });
6768

src/redis.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ export class RedisCacheStore<T> implements CacheStore<T> {
4141
}
4242

4343
async set(key: string, record: StoreEntity<T>): Promise<void> {
44+
const ttl = record.expiresAt - Date.now();
45+
if (ttl <= 0) return Promise.resolve();
46+
4447
const buf = Buffer.from(JSON.stringify(record), "utf8");
4548

4649
if (this.keyTemplate !== null) {
4750
cacheValueSize.observe({ key: this.keyTemplate }, buf.length);
4851
}
4952

50-
await this.client.set(key, buf, "PX", record.expiresAt - Date.now());
53+
await this.client.set(key, buf, "PX", ttl);
5154
}
5255

5356
async delete(key: string): Promise<void> {

0 commit comments

Comments
 (0)