diff --git a/playground/package.json b/playground/package.json index fa84b08..465438c 100644 --- a/playground/package.json +++ b/playground/package.json @@ -3,7 +3,8 @@ "type": "module", "private": true, "scripts": { - "start": "cross-env NODE_NO_WARNINGS=1 node --loader ts-node/esm src/index.ts" + "start": "cross-env NODE_NO_WARNINGS=1 node --loader ts-node/esm src/index.ts", + "start-server": "cross-env NODE_NO_WARNINGS=1 node --loader ts-node/esm src/server.ts" }, "dependencies": { "@aws-sdk/client-dynamodb": "^3.658.1", diff --git a/playground/src/server.ts b/playground/src/server.ts new file mode 100644 index 0000000..a67b0f6 --- /dev/null +++ b/playground/src/server.ts @@ -0,0 +1,33 @@ +import { createServer } from 'node:http' +import { setTimeout } from 'node:timers/promises' +import { BentoCache, bentostore } from 'bentocache' +import { redisDriver } from 'bentocache/drivers/redis' + +const bento = new BentoCache({ + default: 'redis', + stores: { + redis: bentostore().useL2Layer(redisDriver({ connection: { host: '127.0.0.1', port: 6379 } })), + }, +}) + +function getCachedValue() { + return bento.getOrSet({ + key: 'foo', + factory: async () => { + await setTimeout(1000) + return 'bar' + }, + ttl: '4s', + }) +} + +const server = createServer(async (_req, res) => { + res.writeHead(200, { 'Content-Type': 'application/json' }) + + const value = await getCachedValue() + res.end(JSON.stringify(value)) +}) + +server.listen(8042, () => { + console.log('Listening on http://localhost:8042') +})