Skip to content
This repository was archived by the owner on Dec 17, 2022. It is now read-only.

Commit 147b6ad

Browse files
committed
Add caching for generated images.
1 parent 3d5b1cf commit 147b6ad

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

RenderButton.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
11
import {Buffer} from 'node:buffer';
22
import sharp from 'sharp';
33
import {fabric} from 'fabric';
4+
import cacheManager from 'cache-manager';
5+
import fsStore from 'cache-manager-fs-hash';
6+
import hash from 'object-hash';
7+
8+
// Const memoryCache = cacheManager.caching({
9+
// store: 'memory',
10+
// max: 100,
11+
// ttl: 10 * 60, /* Seconds */
12+
// });
13+
14+
const diskCache = cacheManager.caching({
15+
store: fsStore,
16+
options: {
17+
path: 'tmp/streamdeck-images', // Path for cached files
18+
ttl: 60 * 60, // Time to life in seconds
19+
subdirs: true, // Create subdirectories to reduce the
20+
// files in a single dir (default: false)
21+
zip: true, // Zip files to save diskspace (default: false)
22+
},
23+
});
24+
25+
const cache = cacheManager.multiCaching([
26+
// MemoryCache, // not using memory cache in development since the restart is way more common than one may think and on restart it would always rerender everything
27+
diskCache,
28+
]);
429

530
sharp.concurrency(10);
631

732
export default class RenderButton {
833
async render(options = {}) {
34+
try {
35+
return cache.wrap(hash(options), () => RenderButton.prototype.renderImage(options));
36+
} catch (error) {
37+
console.log('error while trying to render image via cache or actually render', error);
38+
}
39+
}
40+
41+
async renderImage(options = {}) {
942
const {keyIndex, label: textString, backgroundColor} = options;
1043
const time = Date.now();
1144
console.log(`render ${keyIndex} ${textString} ${time}`);

0 commit comments

Comments
 (0)