-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lru cache to support range requests
- Loading branch information
Showing
2 changed files
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,55 @@ | ||
import type { Readable } from '@zarrita/storage'; | ||
import QuickLRU from 'quick-lru'; | ||
|
||
export class LRUCacheStore<S extends Readable> { | ||
cache: QuickLRU<string, Promise<Uint8Array | undefined>>; | ||
constructor(public store: S, maxSize: number = 100) { | ||
this.cache = new QuickLRU({ maxSize }); | ||
} | ||
get(...args: Parameters<S['get']>) { | ||
const [key, opts] = args; | ||
if (this.cache.has(key)) { | ||
return this.cache.get(key)!; | ||
type RangeQuery = | ||
| { | ||
offset: number; | ||
length: number; | ||
} | ||
const value = Promise.resolve(this.store.get(key, opts)).catch((err) => { | ||
this.cache.delete(key); | ||
| { | ||
suffixLength: number; | ||
}; | ||
|
||
function normalizeKey(key: string, range?: RangeQuery) { | ||
if (!range) return key; | ||
if ('suffixLength' in range) return `${key}:-${range.suffixLength}`; | ||
return `${key}:${range.offset}:${range.offset + range.length - 1}`; | ||
} | ||
|
||
export function lru<S extends Readable>(store: S, maxSize: number = 100) { | ||
const cache = new QuickLRU<string, Promise<Uint8Array | undefined>>({ maxSize }); | ||
let getRange = store.getRange ? store.getRange.bind(store) : undefined; | ||
function get(...args: Parameters<S['get']>) { | ||
const [key, opts] = args; | ||
const cacheKey = normalizeKey(key); | ||
const cached = cache.get(cacheKey); | ||
if (cached) return cached; | ||
const result = Promise.resolve(store.get(key, opts)).catch((err) => { | ||
cache.delete(cacheKey); | ||
throw err; | ||
}); | ||
this.cache.set(key, value); | ||
return value; | ||
cache.set(cacheKey, result); | ||
return result; | ||
} | ||
if (getRange) { | ||
getRange = (...args: Parameters<NonNullable<S['getRange']>>) => { | ||
const [key, range, opts] = args; | ||
const cacheKey = normalizeKey(key, range); | ||
const cached = cache.get(cacheKey); | ||
if (cached) return cached; | ||
const result = Promise.resolve(getRange!(key, range, opts)).catch((err) => { | ||
cache.delete(cacheKey); | ||
throw err; | ||
}); | ||
cache.set(cacheKey, result); | ||
return result; | ||
}; | ||
} | ||
return new Proxy(store, { | ||
get(target, prop, receiver) { | ||
if (prop === 'get') return get; | ||
if (prop === 'getRange' && getRange) return getRange; | ||
return Reflect.get(target, prop, receiver); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters