Skip to content

Commit

Permalink
chore: prepare better playground
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Feb 11, 2025
1 parent be94325 commit e2b9966
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 158 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"testcontainers": "^10.17.2",
"ts-node": "^10.9.2",
"tsup": "^8.3.6",
"tsx": "^4.19.2",
"typescript": "~5.7.3"
},
"prettier": "@julr/tooling-configs/prettier"
Expand Down
12 changes: 3 additions & 9 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
{
"name": "@bentocache/playground",
"type": "module",
"private": true,
"scripts": {
"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"
"dev": "tsx watch src/index.tsx"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.741.0",
"@bentocache/plugin-prometheus": "workspace:*",
"@hono/node-server": "^1.13.8",
"bentocache": "workspace:*",
"hono": "^4.6.20",
"pino": "^9.6.0"
},
"devDependencies": {
"cross-env": "7.0.3",
"prom-client": "^15.1.3"
}
}
24 changes: 24 additions & 0 deletions playground/src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { pino } from 'pino'
import { BentoCache, bentostore } from 'bentocache'
import { redisDriver } from 'bentocache/drivers/redis'
import { memoryDriver } from 'bentocache/drivers/memory'

export const bento = new BentoCache({
default: 'memoryAndRedis',
logger: pino({
level: 'debug',
transport: {
target: 'pino-pretty',
options: { colorize: true },
},
}),
stores: {
memory: bentostore().useL1Layer(memoryDriver({})),

redis: bentostore().useL2Layer(redisDriver({ connection: { host: 'localhost', port: 6379 } })),

memoryAndRedis: bentostore()
.useL1Layer(memoryDriver({}))
.useL2Layer(redisDriver({ connection: { host: 'localhost', port: 6379 } })),
},
})
55 changes: 0 additions & 55 deletions playground/src/index.ts

This file was deleted.

53 changes: 53 additions & 0 deletions playground/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Hono } from 'hono'
import { logger } from 'hono/logger'
import { serve } from '@hono/node-server'
import { setTimeout } from 'node:timers/promises'

import { bento } from './cache.js'

const app = new Hono().use(logger())

const slowFetcher = async (url: string, timeout: number = 1000) => {
await setTimeout(timeout)
return fetch(url).then((response) => response.json())
}

app.get('/cache-user/:id', async (c) => {
const id = c.req.param('id')
const user = await slowFetcher(`https://jsonplaceholder.typicode.com/users/${id}`)

await bento.set({
ttl: '10s',
key: `user-${id}`,
value: user,
})

return c.html(
<div>
<h1>User {id}</h1>
<p>Cached for 10 seconds</p>
<pre>{JSON.stringify(user, null, 2)}</pre>
</div>,
)
})

app.get('/cached-user/:id', async (c) => {
const id = c.req.param('id')
const user = await bento.get({ key: `user-${id}`, defaultValue: 'NOT CACHED' })

return c.html(
<div>
<h1>User {id}</h1>
<p>From cache</p>
<pre>{JSON.stringify(user, null, 2)}</pre>
</div>,
)
})

app.get('/', (c) => {
return c.text('Hello Hono!')
})

const port = 3042
console.log(`Server is running on http://localhost:${port}`)
serve({ fetch: app.fetch, port })
50 changes: 0 additions & 50 deletions playground/src/prometheus.ts

This file was deleted.

33 changes: 0 additions & 33 deletions playground/src/server.ts

This file was deleted.

11 changes: 8 additions & 3 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"extends": "@adonisjs/tsconfig/tsconfig.package.json",
"compilerOptions": {
"rootDir": "./",
"outDir": "build"
"target": "ESNext",
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx",
"module": "NodeNext",
"types": ["node"],
"strict": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
}
}
46 changes: 38 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e2b9966

Please sign in to comment.