#307 Add ipp.shareCacheTime and cachebusted link after an upload

This commit is contained in:
Alan Grainger
2026-10-19 14:42:43 +13:00
parent 8bf1d753aa
commit a958559247
11 changed files with 159 additions and 11 deletions
+1
View File
@@ -8,6 +8,7 @@
"showHomePage": true,
"allowSlugLinks": true,
"uploadUrl": "/upload",
"shareCacheTime": 120,
"showMetadata": {
"description": {
"caption": false,
+9 -1
View File
@@ -12,6 +12,7 @@ import {
cachedPromise,
fetchSharedLink,
getConfigOption,
getNumericConfigOption,
invalidPasswordResponse,
isKey,
log,
@@ -46,8 +47,15 @@ import { h } from 'preact'
read-only. In-place mutation persists for the cache lifetime and leaks
across concurrent requests. The assets are sorted into display order here,
before caching, so no caller needs to sort them; clone before mutating.
The TTL is `ipp.shareCacheTime`, so it bounds how long a visitor's upload
takes to reach the gallery. It is read on each `set` because this cache is
built at import time, before the config is loaded.
*/
const shareCache = new TtlLruCache<Promise<SharedLinkResult>>({ ttlMs: 120_000, max: 100 })
const shareCache = new TtlLruCache<Promise<SharedLinkResult>>({
ttlMs: () => Math.max(1, getNumericConfigOption('ipp.shareCacheTime', 120)) * 1000,
max: 100
})
/*
Per-asset detail cache for the lazy album flow. When a `needsDetail` album
+92
View File
@@ -0,0 +1,92 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { KeyType } from '@ipp/core'
import { getShareByKey } from '../src/immich'
/*
`ipp.shareCacheTime` sets how long getShareByKey serves a share from memory
before it asks Immich again. Config is mocked per test rather than loaded
from a file.
*/
const cfg: Record<string, unknown> = {}
vi.mock('@ipp/core', async (importOriginal) => ({
...await importOriginal<typeof import('@ipp/core')>(),
getNumericConfigOption: (path: string, fallback: number) => (path in cfg ? Number(cfg[path]) : fallback)
}))
const individualShare = {
type: 'INDIVIDUAL',
key: 'real-key',
allowDownload: true,
expiresAt: null,
showMetadata: true,
assets: [{ id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', type: 'IMAGE', isTrashed: false, thumbhash: 'hashA' }]
}
function stubImmich () {
const fetchMock = vi.fn(async () => ({
ok: true,
status: 200,
headers: { get: (h: string) => (h.toLowerCase() === 'content-type' ? 'application/json' : null) },
json: async () => individualShare
}))
vi.stubGlobal('fetch', fetchMock)
return fetchMock
}
let keyCounter = 0
/** A fresh key per test, because getShareByKey caches by key. */
function uniqueKey () {
return 'cache-key-' + (keyCounter++)
}
describe('ipp.shareCacheTime', () => {
beforeEach(() => {
process.env.IMMICH_URL = 'http://immich.test'
vi.useFakeTimers()
for (const key of Object.keys(cfg)) delete cfg[key]
})
afterEach(() => {
vi.useRealTimers()
vi.unstubAllGlobals()
})
it('keeps a share for 120 seconds by default', async () => {
const fetchMock = stubImmich()
const key = uniqueKey()
await getShareByKey(key, undefined, KeyType.key)
vi.advanceTimersByTime(119_999)
await getShareByKey(key, undefined, KeyType.key)
expect(fetchMock).toHaveBeenCalledTimes(1)
vi.advanceTimersByTime(1)
await getShareByKey(key, undefined, KeyType.key)
expect(fetchMock).toHaveBeenCalledTimes(2)
})
it('uses the configured time', async () => {
cfg['ipp.shareCacheTime'] = 15
const fetchMock = stubImmich()
const key = uniqueKey()
await getShareByKey(key, undefined, KeyType.key)
vi.advanceTimersByTime(14_999)
await getShareByKey(key, undefined, KeyType.key)
expect(fetchMock).toHaveBeenCalledTimes(1)
vi.advanceTimersByTime(1)
await getShareByKey(key, undefined, KeyType.key)
expect(fetchMock).toHaveBeenCalledTimes(2)
})
it('treats a value under 1 as 1 second', async () => {
cfg['ipp.shareCacheTime'] = 0
const fetchMock = stubImmich()
const key = uniqueKey()
// Concurrent misses still share one Immich call
await Promise.all([getShareByKey(key, undefined, KeyType.key), getShareByKey(key, undefined, KeyType.key)])
expect(fetchMock).toHaveBeenCalledTimes(1)
vi.advanceTimersByTime(999)
await getShareByKey(key, undefined, KeyType.key)
expect(fetchMock).toHaveBeenCalledTimes(1)
vi.advanceTimersByTime(1)
await getShareByKey(key, undefined, KeyType.key)
expect(fetchMock).toHaveBeenCalledTimes(2)
})
})