Skip to content

Commit

Permalink
262nd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Feb 19, 2024
1 parent b8c3b95 commit f437471
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ This seed repository provides the following features:

- [x] [Hello World](./src/routes/hello-world)
- [x] [CRUD Operations](./src/routes/todos)
- [x] [Authentication](./src/routes/auth)
- [x] [JWT Authentication](./src/routes/auth)
- [x] [One-time Password](./src/routes/auth/otp)
- [x] [File Uploads](./src/routes/file-uploads)
- [x] [Real-time Data](./src/routes/echo)
Expand All @@ -152,6 +152,7 @@ This seed repository provides the following features:
- [x] [Background Workers](./src/routes/queues)
- [x] [Cron Jobs](./src/routes/queues)
- [x] [Caching](./src/routes/hello-world/caching-memory)
- [x] [Cache Deduplication](./src/routes/hello-world/caching-dedupe/[id]-memory)

## Configuration

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@fastify/websocket": "^8.3.1",
"@sinclair/typebox": "^0.32.14",
"@tirke/node-cache-manager-ioredis": "^3.6.0",
"async-cache-dedupe": "^2.1.0",
"bullmq": "^5.1.11",
"cache-manager": "^5.4.0",
"exceljs": "^4.4.0",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

51 changes: 51 additions & 0 deletions src/routes/hello-world/caching-dedupe/[id]-memory/+handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
import { createCache } from 'async-cache-dedupe';

export default (async (app) => {
const cache = createCache({ ttl: 5 });

const useCache = cache.define('memoryText', async (id) => {
console.log('id =', id);
return { message: 'OK', id };
});

/**
* ```sh
* # this will trigger the log: id = 1
* $ curl --request GET --url http://127.0.0.1:3000/api/hello-world/caching-dedupe/1-memory
* # response: { "message": "OK", "id": "1" }
*
* # this won't trigger the log
* $ curl --request GET --url http://127.0.0.1:3000/api/hello-world/caching-dedupe/1-memory
* # response: { "message": "OK", "id": "1" }
*
* # this will trigger the log: id = 2
* $ curl --request GET --url http://127.0.0.1:3000/api/hello-world/caching-dedupe/2-memory
* # response: { "message": "OK", "id": "2" }
* ```
*/
app.get(
'',
{
schema: {
params: Type.Object({
id: Type.String(),
}),
response: {
200: Type.Object({
message: Type.String(),
id: Type.String(),
}),
},
},
},
async (req, reply) => {
const { id } = req.params;

const cached = await useCache.memoryText(id);

return reply.send(cached);
},
);
}) as FastifyPluginAsyncTypebox;

0 comments on commit f437471

Please sign in to comment.