Skip to content

Commit 5885a8c

Browse files
committed
feat: msgpackr default encoder
1 parent 8e60ee2 commit 5885a8c

File tree

6 files changed

+69
-22
lines changed

6 files changed

+69
-22
lines changed

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
node_modules
2+
3+
.nyc_output
4+
coverage
5+
26
tests
7+
38
bunfig.toml
49
.prettierrc.json

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
- Runs on top of [bun-sqlite](https://bun.sh/docs/api/sqlite)
44
- Optimized `mset`/`mget` support
5-
- Supports CBOR for efficient and fast storage (selectable between `json` or `cbor`, default: `cbor`)
5+
- Multiple encoders support: `msgpackr`, `cbor`, `json`
6+
- Auto purge (clean expired records every hour)
67

78
## Installation
89

@@ -20,7 +21,7 @@ import bunSqliteStore from 'cache-manager-bun-sqlite3';
2021

2122
// SQLite :memory: cache store
2223
cache = await cacheManager.caching(bunSqliteStore, {
23-
serializer: 'json', // default is 'cbor'
24+
serializer: 'json', // default is 'msgpackr'
2425
ttl: 20, // TTL in seconds
2526
});
2627

@@ -31,11 +32,11 @@ const cache = await cacheManager.caching(bunSqliteStore, {
3132
});
3233

3334
// TTL in seconds
34-
await cache.set('foo', { test: 'bar' }, 600);
35+
await cache.set('foo', { test: 'bar' }, 30);
3536
const value = await cache.get('foo');
3637

3738
// TTL in seconds
38-
await cache.set('foo', { test: 'bar' }, 600);
39+
await cache.set('foo', { test: 'bar' }, 30);
3940
const value = await cache.get('foo');
4041
```
4142

@@ -75,6 +76,4 @@ await multiCache.mset('foo1', 'bar1', 'foo3', 'bar3', customTTL);
7576
await multiCache.mget('foo1', 'foo3');
7677
```
7778

78-
## Fork from
79-
80-
[node-cache-manager-sqlite](https://github.com/maxpert/node-cache-manager-sqlite)
79+
Fork from [node-cache-manager-sqlite](https://github.com/maxpert/node-cache-manager-sqlite)

bun.lockb

3.86 KB
Binary file not shown.

index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Database } from 'bun:sqlite';
2-
import { decode, encode } from 'cbor-x';
32
import type { Store, Config } from 'cache-manager';
43

4+
import * as cbor from 'cbor-x';
5+
import * as msgpackr from 'msgpackr';
6+
57
const configurePragmas = `
68
PRAGMA main.synchronous = NORMAL;
79
PRAGMA main.journal_mode = WAL2;
@@ -31,15 +33,19 @@ const serializers = {
3133
deserialize: JSON.parse,
3234
},
3335
cbor: {
34-
serialize: encode,
35-
deserialize: decode,
36+
serialize: cbor.encode,
37+
deserialize: cbor.decode,
38+
},
39+
msgpackr: {
40+
serialize: msgpackr.pack,
41+
deserialize: msgpackr.unpack,
3642
},
3743
};
3844

3945
type SqliteCacheOptions = Config & {
4046
name?: string;
4147
path?: string;
42-
serializer?: 'json' | 'cbor';
48+
serializer?: 'msgpackr' | 'cbor' | 'json';
4349
ttl?: number;
4450
};
4551

@@ -51,13 +57,13 @@ export interface SqliteStore extends Store {
5157

5258
export class NoCacheableError implements Error {
5359
name = 'NoCacheableError';
54-
constructor(public message: string) { }
60+
constructor(public message: string) {}
5561
}
5662

5763
export default async function bunSqliteStore({
5864
name = 'cache',
5965
path = ':memory:',
60-
serializer = 'cbor',
66+
serializer = 'msgpackr',
6167
ttl = 24 * 60 * 60, // 1 day in seconds
6268
...options
6369
}: SqliteCacheOptions = {}): Promise<SqliteStore> {
@@ -200,5 +206,5 @@ export default async function bunSqliteStore({
200206
get client() {
201207
return db;
202208
},
203-
} as SqliteStore;
209+
};
204210
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "cache-manager-bun-sqlite3",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "Bun sqlite store for node-cache-manager",
55
"type": "module",
66
"repository": {
77
"type": "git",
8-
"url": "https://github.com/sjdonado/node-cache-manager-sqlite.git"
8+
"url": "https://github.com/sjdonado/cache-manager-bun-sqlite3.git"
99
},
1010
"keywords": [
1111
"cache-manager",
@@ -14,7 +14,8 @@
1414
"author": "Juan Rodriguez",
1515
"license": "MIT",
1616
"dependencies": {
17-
"cbor-x": "^1.5.9"
17+
"cbor-x": "^1.5.9",
18+
"msgpackr": "^1.11.0"
1819
},
1920
"devDependencies": {
2021
"bun-types": "^1.1.20",

tests/serializers.test.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,58 @@ import cacheManager from 'cache-manager';
44
import sqliteStore from '../index.js';
55

66
describe('Serializers', () => {
7-
it('supports CBOR', async () => {
7+
it('supports msgpackr', async () => {
8+
const cache = await cacheManager.caching(sqliteStore, {
9+
serializer: 'msgpackr',
10+
});
11+
12+
const value = {
13+
foo: 'bar',
14+
arr: [1, true, null],
15+
id: '2KvHC9z14GSl4YpkNMX384',
16+
description: 'Test · Unicode · Description',
17+
hash: 'https://hash/ab67616d0000b2734f0fd9dad63977146e685700',
18+
link: 'https://a.test.com/path/df989a31c8233f46b6a997c59025f9c8021784aa',
19+
};
20+
21+
await cache.set('foo', value);
22+
23+
expect(await cache.get('foo')).toEqual(value);
24+
});
25+
26+
it('supports cbor', async () => {
827
const cache = await cacheManager.caching(sqliteStore, {
928
serializer: 'cbor',
1029
});
1130

12-
await cache.set('foo', { foo: 'bar', arr: [1, true, null] });
13-
expect(await cache.get('foo')).toEqual({ foo: 'bar', arr: [1, true, null] });
31+
const value = {
32+
foo: 'bar',
33+
arr: [1, true, null],
34+
id: '2KvHC9z14GSl4YpkNMX384',
35+
};
36+
37+
await cache.set('foo', value);
38+
39+
expect(await cache.get('foo')).toEqual(value);
1440
});
1541

1642
it('supports JSON', async () => {
1743
const cache = await cacheManager.caching(sqliteStore, {
1844
serializer: 'json',
1945
});
2046

21-
await cache.set('foo', { foo: 'bar', arr: [1, true, null] });
22-
expect(await cache.get('foo')).toEqual({ foo: 'bar', arr: [1, true, null] });
47+
const value = {
48+
foo: 'bar',
49+
arr: [1, true, null],
50+
id: '2KvHC9z14GSl4YpkNMX384',
51+
description: 'Test · Unicode · Description',
52+
hash: 'https://hash/ab67616d0000b2734f0fd9dad63977146e685700',
53+
link: 'https://a.test.com/path/df989a31c8233f46b6a997c59025f9c8021784aa',
54+
};
55+
56+
await cache.set('foo', value);
57+
58+
expect(await cache.get('foo')).toEqual(value);
2359
});
2460

2561
it('should throw NoCacheableError when setting a bad object', async () => {

0 commit comments

Comments
 (0)