refactor: update HEIC conversion logic and cache management

- Increased the cache size for HEIC images from 5 to 10 to improve performance.
- Refactored the cache key generation to use the source string instead of file properties.
- Updated the HEIC image processing function to accept the original URL as a parameter.
- Removed unused video cache management functions to streamline the codebase.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-06-05 18:39:17 +08:00
parent 2a9990509b
commit bd6be8ba6e
3 changed files with 16 additions and 50 deletions

View File

@@ -21,7 +21,7 @@ const heicCache: LRUCache<string, ConversionResult> = new LRUCache<
string,
ConversionResult
>(
5, // Smaller cache size for images as they might be larger
10, // Smaller cache size for images as they might be larger
(value, key, reason) => {
try {
URL.revokeObjectURL(value.url)
@@ -33,18 +33,13 @@ const heicCache: LRUCache<string, ConversionResult> = new LRUCache<
)
/**
* 生成文件的缓存键
* 生成文件的缓存键(基于 src
*/
function generateCacheKey(
file: File | Blob,
options: HeicConversionOptions,
): string {
const { size } = file
const { type } = file
function generateCacheKey(src: string, options: HeicConversionOptions): string {
const quality = options.quality || 1
const format = options.format || 'image/jpeg'
// 使用文件大小、类型和转换选项生成唯一键
return `${type}-${size}-${quality}-${format}`
// 使用文件 src 和转换选项生成唯一键
return `${src}-${quality}-${format}`
}
/**
@@ -72,17 +67,18 @@ export const isBrowserSupportHeic = () => {
*/
export async function convertHeicImage(
file: File | Blob,
src: string,
options: HeicConversionOptions = {},
): Promise<ConversionResult> {
const { quality = 1, format = 'image/jpeg' } = options
// 生成缓存键
const cacheKey = generateCacheKey(file, options)
const cacheKey = generateCacheKey(src, options)
// 检查缓存
const cachedResult = heicCache.get(cacheKey)
if (cachedResult) {
console.info('Using cached HEIC conversion result')
console.info('Using cached HEIC conversion result', cachedResult)
return cachedResult
}
@@ -159,12 +155,12 @@ export function getHeicCacheStats(): {
}
/**
* 根据文件和选项移除特定的 HEIC 缓存项
* 根据 src 和选项移除特定的 HEIC 缓存项
*/
export function removeHeicCacheByFile(
file: File | Blob,
export function removeHeicCacheBySrc(
src: string,
options: HeicConversionOptions = {},
): boolean {
const cacheKey = generateCacheKey(file, options)
const cacheKey = generateCacheKey(src, options)
return heicCache.delete(cacheKey)
}

View File

@@ -203,7 +203,7 @@ export class ImageLoaderManager {
})
if (shouldHeicTransformed) {
return await this.processHeicImage(blob, callbacks)
return await this.processHeicImage(blob, originalUrl, callbacks)
} else {
return this.processRegularImage(blob, originalUrl, callbacks) // 传递原始 URL
}
@@ -216,6 +216,7 @@ export class ImageLoaderManager {
private async processHeicImage(
blob: Blob,
originalUrl: string,
callbacks: LoadingCallbacks,
): Promise<ImageLoadResult> {
const { onError: _onError, onLoadingStateUpdate } = callbacks
@@ -230,7 +231,7 @@ export class ImageLoaderManager {
// 动态导入 heic-converter 模块
const { convertHeicImage } = await import('~/lib/heic-converter')
const conversionResult = await convertHeicImage(blob)
const conversionResult = await convertHeicImage(blob, originalUrl)
// Hide loading indicator
onLoadingStateUpdate?.({
@@ -271,7 +272,7 @@ export class ImageLoaderManager {
// 检查缓存
const cachedResult = regularImageCache.get(cacheKey)
if (cachedResult) {
console.info('Using cached regular image result')
console.info('Using cached regular image result', cachedResult)
// Hide loading indicator
onLoadingStateUpdate?.({

View File

@@ -30,37 +30,6 @@ const videoCache: LRUCache<string, ConversionResult> = new LRUCache<
}
})
// Export cache management functions
export function getVideoCacheSize(): number {
return videoCache.size()
}
export function clearVideoCache(): void {
videoCache.clear()
}
export function getCachedVideo(url: string): ConversionResult | undefined {
return videoCache.get(url)
}
/**
* Remove a specific video from cache and clean up its blob URL
*/
export function removeCachedVideo(url: string): boolean {
return videoCache.delete(url)
}
/**
* Get detailed cache statistics for debugging
*/
export function getVideoCacheStats(): {
size: number
maxSize: number
keys: string[]
} {
return videoCache.getStats()
}
// 检查 WebCodecs 支持
export function isWebCodecsSupported(): boolean {
return (