feat(core): implement generic LRU-TTL cache and integrate with S3 presigned URLs

This commit is contained in:
yiling
2026-06-08 14:52:56 +08:00
parent fab10d14ab
commit ea01d91fa4
4 changed files with 350 additions and 2 deletions
+57
View File
@@ -27,6 +27,14 @@ vi.mock('@aws-sdk/s3-request-presigner', () => {
})
describe('S3Service implementations', () => {
beforeEach(async () => {
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
vi.mocked(getSignedUrl).mockReset()
vi.mocked(getSignedUrl).mockResolvedValue('http://presigned-url')
vi.clearAllMocks()
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
describe('S3StorageService', () => {
it('should correctly build standard endpoints', () => {
const s3 = new S3StorageService('s3.example.com', 'key', 'secret', 'test-bucket', true)
@@ -57,6 +65,55 @@ describe('S3Service implementations', () => {
const url = await s3.presign('bucket', 'key', 'GET')
expect(url).toBe('http://presigned-url')
})
it('should cache GET presign URLs', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
vi.mocked(getSignedUrl).mockClear()
vi.mocked(getSignedUrl).mockResolvedValueOnce('http://presigned-url-1')
vi.mocked(getSignedUrl).mockResolvedValueOnce('http://presigned-url-2')
const url1 = await s3.presign('bucket', 'key', 'GET')
const url2 = await s3.presign('bucket', 'key', 'GET')
expect(url1).toBe('http://presigned-url-1')
expect(url2).toBe('http://presigned-url-1')
expect(vi.mocked(getSignedUrl)).toHaveBeenCalledTimes(1)
})
it('should NOT cache PUT presign URLs', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
vi.mocked(getSignedUrl).mockClear()
vi.mocked(getSignedUrl).mockResolvedValueOnce('http://put-url-1')
vi.mocked(getSignedUrl).mockResolvedValueOnce('http://put-url-2')
const url1 = await s3.presign('bucket', 'key', 'PUT')
const url2 = await s3.presign('bucket', 'key', 'PUT')
expect(url1).toBe('http://put-url-1')
expect(url2).toBe('http://put-url-2')
expect(vi.mocked(getSignedUrl)).toHaveBeenCalledTimes(2)
})
it('should respect PRESIGNED_URL_EXPIRES_IN env', async () => {
process.env.PRESIGNED_URL_EXPIRES_IN = '10'
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner')
vi.mocked(getSignedUrl).mockClear()
await s3.presign('bucket', 'key', 'GET')
expect(vi.mocked(getSignedUrl)).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining({ expiresIn: 10 * 3600 }),
)
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
})
describe('LocalStorageService', () => {
+22 -2
View File
@@ -14,6 +14,7 @@ import * as fs from 'fs'
import { readFileSync, statSync } from 'fs'
import * as path from 'path'
import { ulid } from 'ulid'
import { LruTtlCache } from '../cache/lru-ttl-cache'
export function signLocalUrl(bucket: string, key: string): string {
const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret'
@@ -69,6 +70,7 @@ export interface S3Service {
export class S3StorageService implements S3Service {
private client: S3Client
private bucket: string
private presignCache = new LruTtlCache<string, string>(50000)
constructor(
endpoint: string,
@@ -269,6 +271,19 @@ export class S3StorageService implements S3Service {
}
async presign(bucket: string, key: string, method: string): Promise<string> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const expiresInSeconds = expireHours * 3600
// Cache time is 2/3 of expire time, rounded to minute
const cacheMinutes = Math.round((expireHours * 60 * 2) / 3)
const cacheTtlMs = cacheMinutes * 60 * 1000
const cacheKey = `${bucket}:${key}`
if (method === 'GET') {
const cached = this.presignCache.get(cacheKey)
if (cached) return cached
}
let command: GetObjectCommand | PutObjectCommand
if (method === 'GET') {
command = new GetObjectCommand({
@@ -284,8 +299,13 @@ export class S3StorageService implements S3Service {
throw new Error(`Invalid method: ${method}`)
}
// 1 hour expiration
return await getSignedUrl(this.client, command, { expiresIn: 3600 })
const url = await getSignedUrl(this.client, command, { expiresIn: expiresInSeconds })
if (method === 'GET') {
this.presignCache.set(cacheKey, url, cacheTtlMs)
}
return url
}
}