diff options
author | Paul Frazee <pfrazee@gmail.com> | 2023-03-21 12:59:10 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-21 12:59:10 -0500 |
commit | 858d4c8c8811ca8e16bffe3bfe0d541e576177ec (patch) | |
tree | 8199e1029edb7161c34fb59806399c30fcdd61a3 /src/state/models/cache/link-metas.ts | |
parent | c1d454b7cfe6a7ec107b39483df9cb0fb64e1c76 (diff) | |
download | voidsky-858d4c8c8811ca8e16bffe3bfe0d541e576177ec.tar.zst |
* Introduce an image sizes cache to improve feed layouts (close #213) * Clear out resolved promises from the image cache
Diffstat (limited to 'src/state/models/cache/link-metas.ts')
-rw-r--r-- | src/state/models/cache/link-metas.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/state/models/cache/link-metas.ts b/src/state/models/cache/link-metas.ts new file mode 100644 index 000000000..607968c80 --- /dev/null +++ b/src/state/models/cache/link-metas.ts @@ -0,0 +1,44 @@ +import {makeAutoObservable} from 'mobx' +import {LRUMap} from 'lru_map' +import {RootStoreModel} from '../root-store' +import {LinkMeta, getLinkMeta} from 'lib/link-meta/link-meta' + +type CacheValue = Promise<LinkMeta> | LinkMeta +export class LinkMetasCache { + cache: LRUMap<string, CacheValue> = new LRUMap(100) + + constructor(public rootStore: RootStoreModel) { + makeAutoObservable( + this, + { + rootStore: false, + cache: false, + }, + {autoBind: true}, + ) + } + + // public api + // = + + async getLinkMeta(url: string) { + const cached = this.cache.get(url) + if (cached) { + try { + return await cached + } catch (e) { + // ignore, we'll try again + } + } + try { + const promise = getLinkMeta(this.rootStore, url) + this.cache.set(url, promise) + const res = await promise + this.cache.set(url, res) + return res + } catch (e) { + this.cache.delete(url) + throw e + } + } +} |