Skip to content

Commit

Permalink
feat: Move to keyv as cache implementation.
Browse files Browse the repository at this point in the history
Move to `keyv` as cache implementation. This makes it compatible with
various cache implementations. To update replace the `client` option
with `store` and pass a `Keyv` instance.
  • Loading branch information
mvantellingen committed Apr 30, 2024
1 parent 96da1c1 commit 3fbea90
Show file tree
Hide file tree
Showing 6 changed files with 3,538 additions and 2,836 deletions.
36 changes: 36 additions & 0 deletions .changeset/clean-bulldogs-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
'@labdigital/dataloader-cache-wrapper': minor
---

Move to `keyv` as cache implementation. This makes it compatible with various
cache implementations. To update replace the `client` option with `store` and
pass a `Keyv` instance.

```ts


import { dataloaderCache } from "@labdigital/dataloader-cache-wrapper"
import Keyv from 'keyv';

const redis = new Redis('redis://user:pass@localhost:6379');
const keyvRedis = new KeyvRedis(redis);

export const createProductBySlugLoader = () => {
return new DataLoader<ProductReference, any>(ProductDataLoader, {
maxBatchSize: 50,
});
};

export const ProductDataLoader = async
(keys: readonly any[]): Promise<(Product | null)[]> => {
return dataloaderCache(_uncachedProductDataLoader, keys, {
store: new Keyv({ store: keyvRedis }),
ttl: 3600,

cacheKeysFn: (ref: ProductRef) => {
const key = `${ref.store}-${ref.locale}-${ref.currency}`;
return [`some-data:${key}:id:${ref.slug}`];
},
})
}
```
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const createProductBySlugLoader = () => {

export const ProductDataLoader = async (keys: readonly any[]): Promise<(Product | null)[]> => {
return dataloaderCache(_uncachedProductDataLoader, keys, {
client: redisClient,
store: new Keyv(),
ttl: 3600,

cacheKeysFn: (ref: ProductRef) => {
Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,21 @@
"devDependencies": {
"@changesets/cli": "^2.26.1",
"@labdigital/eslint-config-node": "^0.0.5",
"@types/ioredis-mock": "^8.2.5",
"@types/object-hash": "^3.0.6",
"@vitest/coverage-v8": "^0.33.0",
"dataloader": "^2.2.2",
"eslint-plugin-unused-imports": "^2.0.0",
"eslint": "^8.40.0",
"ioredis-mock": "^8.9.0",
"ioredis": "^5.3.2",
"eslint-plugin-unused-imports": "^2.0.0",
"tsup": "6.7.0",
"typescript": "5.0.4",
"vitest": "^0.33.0"
},
"peerDependencies": {
"dataloader": "^2.2.2",
"ioredis": "^5.3.2"
"keyv": "^4.5.4"
},
"dependencies": {
"object-hash": "^3.0.0"
}
},
"packageManager": "pnpm@8.15.7+sha512.c85cd21b6da10332156b1ca2aa79c0a61ee7ad2eb0453b88ab299289e9e8ca93e6091232b25c07cbf61f6df77128d9c849e5c9ac6e44854dbd211c49f3a67adc"
}
Loading

0 comments on commit 3fbea90

Please sign in to comment.