Skip to content

Commit

Permalink
chore(server): add caching for a week
Browse files Browse the repository at this point in the history
  • Loading branch information
thatkookooguy committed Nov 17, 2024
1 parent 9b850a6 commit 46cf475
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 13 deletions.
66 changes: 65 additions & 1 deletion simple-server/package-lock.json

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

5 changes: 4 additions & 1 deletion simple-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"dependencies": {
"@kibibit/kb-hologram": "^2.1.0-next.4",
"body-parser": "^1.20.3",
"cache-manager": "^6.1.3",
"cacheable": "^1.8.4",
"express": "^4.21.1",
"handlebars": "^4.7.8"
"handlebars": "^4.7.8",
"keyv": "^5.2.1"
}
}
61 changes: 50 additions & 11 deletions simple-server/src/simple-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { IKbHologramBaseOptions, KbHologram, KbHologramResultType } from "@kibib
import express, { Request, Response } from 'express';
import bodyParser from "body-parser";
import { join } from "path";
import { createCache } from "cache-manager";
import { CacheableMemory } from "cacheable";
import { Keyv } from 'keyv';

const app = express();
const port = 3000;
Expand All @@ -12,22 +15,58 @@ app.use(bodyParser.urlencoded());
// parse application/json
app.use(bodyParser.json());

// Create a memory cache with a TTL of 1 week
const memoryCache = createCache({
stores: [
new Keyv({
store: new CacheableMemory({ ttl: 7 * 24 * 60 * 60, lruSize: 5000 }),
})
],
ttl: 7 * 24 * 60 * 60 // TTL in seconds (1 week)
});

app.get('/', (req: Request, res: Response) => {
res.send('Hello World!');
});

app.post('/', async (req: Request, res: Response) => {
const kbHologramOptions = getKbHologramOptions(req.body);
const kbHologram = new KbHologram(kbHologramOptions);

const pngBuffer = await kbHologram.render(KbHologramResultType.PngBuffer);

res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': pngBuffer.length
});

res.end(pngBuffer);
const cacheKey = JSON.stringify(req.body);

try {
// Check if the image is cached
const cachedImage: Buffer | null = await memoryCache.get(cacheKey);

if (cachedImage) {
console.log('Cache hit');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': cachedImage.length
});

res.end(cachedImage);

return;
}

console.log('Cache miss');
// Generate a new image if not cached
const kbHologramOptions = getKbHologramOptions(req.body);
const kbHologram = new KbHologram(kbHologramOptions);
const pngBuffer = await kbHologram.render(KbHologramResultType.PngBuffer);

// Cache the generated image
await memoryCache.set(cacheKey, pngBuffer);

res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': pngBuffer.length
});
res.end(pngBuffer);

} catch (error) {
console.error('Error generating image:', error);
res.status(500).send('Error generating image');
}
});

app.listen(port, () => {
Expand Down

0 comments on commit 46cf475

Please sign in to comment.