Skip to content

Commit

Permalink
feat: webdav的exists检查给个缓存,减轻alist压力
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Sep 11, 2024
1 parent b7b52c3 commit 22b72b5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/storage/webdav.storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import colors from 'colors/safe.js'
import type {Request, Response} from 'express'
import Keyv from 'keyv'
import ms from 'ms'
import {Agent} from 'node:https'
import pMap from 'p-map'
import {join} from 'path'
Expand Down Expand Up @@ -27,6 +29,10 @@ export class WebdavStorage implements IStorage {
protected files = new Map<string, {size: number; path: string}>()
protected emptyFiles = new Set<string>()

protected existsCache = new Keyv({
ttl: ms('1h'),
})

constructor(storageConfig: unknown) {
try {
this.storageConfig = storageConfigSchema.parse(storageConfig)
Expand Down Expand Up @@ -78,7 +84,14 @@ export class WebdavStorage implements IStorage {
}

public async exists(path: string): Promise<boolean> {
return await this.client.exists(join(this.basePath, path))
if (await this.existsCache.has(path)) {
return true
}
const exists = await this.client.exists(join(this.basePath, path))
if (exists) {
await this.existsCache.set(path, true)
}
return exists
}

public getAbsolutePath(path: string): string {
Expand Down

0 comments on commit 22b72b5

Please sign in to comment.