|
1 | 1 | import {Buffer} from 'node:buffer';
|
2 | 2 | import sharp from 'sharp';
|
3 | 3 | 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 | +]); |
4 | 29 |
|
5 | 30 | sharp.concurrency(10);
|
6 | 31 |
|
7 | 32 | export default class RenderButton {
|
8 | 33 | 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 = {}) { |
9 | 42 | const {keyIndex, label: textString, backgroundColor} = options;
|
10 | 43 | const time = Date.now();
|
11 | 44 | console.log(`render ${keyIndex} ${textString} ${time}`);
|
|
0 commit comments