mirror of
https://github.com/immich-app/immich.git
synced 2025-12-17 09:13:17 +03:00
Compare commits
1 Commits
chore/log-
...
push-ukqkw
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99b1073143 |
52
web/src/lib/managers/AssetCacheManager.svelte.ts
Normal file
52
web/src/lib/managers/AssetCacheManager.svelte.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { getAssetInfo, getAssetOcr, type AssetOcrResponseDto, type AssetResponseDto } from '@immich/sdk';
|
||||||
|
|
||||||
|
class AsyncCache<V> {
|
||||||
|
#cache = new Map<string, V>();
|
||||||
|
|
||||||
|
async getOrFetch<K>(
|
||||||
|
params: K,
|
||||||
|
fetcher: (params: K) => Promise<V>,
|
||||||
|
keySerializer: (params: K) => string = (params) => JSON.stringify(params),
|
||||||
|
): Promise<V> {
|
||||||
|
const cacheKey = keySerializer(params);
|
||||||
|
|
||||||
|
const cached = this.#cache.get(cacheKey);
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = await fetcher(params);
|
||||||
|
if (value) {
|
||||||
|
this.#cache.set(cacheKey, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this.#cache.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AssetCacheManager {
|
||||||
|
#assetCache = new AsyncCache<AssetResponseDto>();
|
||||||
|
#ocrCache = new AsyncCache<AssetOcrResponseDto[]>();
|
||||||
|
|
||||||
|
async getAsset(assetIdentifier: { key?: string; slug?: string; id: string }) {
|
||||||
|
return this.#assetCache.getOrFetch(assetIdentifier, getAssetInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAssetOcr(id: string) {
|
||||||
|
return this.#ocrCache.getOrFetch({ id }, getAssetOcr, (params) => params.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearAssetCache() {
|
||||||
|
this.#assetCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
clearOcrCache() {
|
||||||
|
this.#ocrCache.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const assetCacheManager = new AssetCacheManager();
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
|
import type { RouteId } from '$app/types';
|
||||||
import { AppRoute } from '$lib/constants';
|
import { AppRoute } from '$lib/constants';
|
||||||
import { getAssetInfo } from '@immich/sdk';
|
import { assetCacheManager } from '$lib/managers/AssetCacheManager.svelte';
|
||||||
import type { NavigationTarget } from '@sveltejs/kit';
|
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
|
|
||||||
export type AssetGridRouteSearchParams = {
|
export type AssetGridRouteSearchParams = {
|
||||||
@@ -20,11 +20,12 @@ export const isAlbumsRoute = (route?: string | null) => !!route?.startsWith('/(u
|
|||||||
export const isPeopleRoute = (route?: string | null) => !!route?.startsWith('/(user)/people/[personId]');
|
export const isPeopleRoute = (route?: string | null) => !!route?.startsWith('/(user)/people/[personId]');
|
||||||
export const isLockedFolderRoute = (route?: string | null) => !!route?.startsWith('/(user)/locked');
|
export const isLockedFolderRoute = (route?: string | null) => !!route?.startsWith('/(user)/locked');
|
||||||
|
|
||||||
export const isAssetViewerRoute = (target?: NavigationTarget | null) =>
|
export const isAssetViewerRoute = (
|
||||||
!!(target?.route.id?.endsWith('/[[assetId=id]]') && 'assetId' in (target?.params || {}));
|
target?: { route?: { id?: RouteId | null }; params?: Record<string, string> | null } | null,
|
||||||
|
) => !!(target?.route?.id?.endsWith('/[[assetId=id]]') && 'assetId' in (target?.params || {}));
|
||||||
|
|
||||||
export function getAssetInfoFromParam({ assetId, slug, key }: { assetId?: string; key?: string; slug?: string }) {
|
export function getAssetInfoFromParam({ assetId, slug, key }: { assetId?: string; key?: string; slug?: string }) {
|
||||||
return assetId ? getAssetInfo({ id: assetId, slug, key }) : undefined;
|
return assetId ? assetCacheManager.getAsset({ id: assetId, slug, key }) : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function currentUrlWithoutAsset() {
|
function currentUrlWithoutAsset() {
|
||||||
|
|||||||
Reference in New Issue
Block a user