Skip to content

Commit

Permalink
chore: add server playground
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Oct 2, 2024
1 parent d0024dd commit 7613081
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions playground/src/server.ts
Original file line number Diff line number Diff line change
@@ -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')
})

0 comments on commit 7613081

Please sign in to comment.