Use an LRU for the resolved media instead of growing unbounded.

This commit is contained in:
Dermot Duffy
2021-10-26 14:04:37 -07:00
parent 38af2f3d62
commit d727593ed8
3 changed files with 17 additions and 6 deletions
+1
View File
@@ -22,6 +22,7 @@
"embla-carousel": "^5.0.1", "embla-carousel": "^5.0.1",
"home-assistant-js-websocket": "^5.11.1", "home-assistant-js-websocket": "^5.11.1",
"lit": "^2.0.2", "lit": "^2.0.2",
"quick-lru": "github:sindresorhus/quick-lru",
"screenfull": "^5.1.0", "screenfull": "^5.1.0",
"zod": "^3.10.0" "zod": "^3.10.0"
}, },
+2 -1
View File
@@ -34,7 +34,8 @@
"label": "Frigate label/object filter (Optional)", "label": "Frigate label/object filter (Optional)",
"live_provider": "Live view provider (Optional)", "live_provider": "Live view provider (Optional)",
"live_preload": "Preload live view", "live_preload": "Preload live view",
"image": "Static image URL for image view (Optional)" "image": "Static image URL for image view (Optional)",
"lazy_load": "Lazily load event media"
}, },
"menu": { "menu": {
"frigate": "Frigate Menu / Default View", "frigate": "Frigate Menu / Default View",
+14 -5
View File
@@ -6,24 +6,33 @@ import {
ResolvedMedia, ResolvedMedia,
resolvedMediaSchema, resolvedMediaSchema,
} from './types.js'; } from './types.js';
import QuickLRU from 'quick-lru';
// It's important the cache size be at least as large as the largest likely
// media query or media items will from a given query will be evicted for other
// items in the same query (which would result in only partial results being
// returned to the user).
// Note: Each entry is about 400 bytes.
const RESOLVED_MEDIA_CACHE_SIZE = 1000;
export class ResolvedMediaCache { export class ResolvedMediaCache {
protected _cache: Record<string, ResolvedMedia>; protected _cache: QuickLRU<string, ResolvedMedia>;
constructor() { constructor() {
this._cache = {}; this._cache = new QuickLRU({maxSize: RESOLVED_MEDIA_CACHE_SIZE});
} }
public has(id: string): boolean { public has(id: string): boolean {
return id in this._cache; return this._cache.has(id);
} }
public get(id: string): ResolvedMedia | undefined { public get(id: string): ResolvedMedia | undefined {
return this._cache[id]; return this._cache.get(id);
} }
public set(id: string, resolvedMedia: ResolvedMedia): void { public set(id: string, resolvedMedia: ResolvedMedia): void {
this._cache[id] = resolvedMedia; this._cache.set(id, resolvedMedia);
} }
} }