Skip to content

Commit

Permalink
feat(lru-cache): upgrade to lru-cache v9
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 16, 2023
1 parent 9f4d111 commit 5b8fc62
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"h3": "^1.6.4",
"ioredis": "^5.3.2",
"listhen": "^1.0.4",
"lru-cache": "^7.18.3",
"lru-cache": "^9.0.3",
"mri": "^1.2.0",
"node-fetch-native": "^1.1.0",
"ofetch": "^1.0.1",
Expand Down
10 changes: 8 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 8 additions & 18 deletions src/drivers/lru-cache.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import { defineDriver } from "./utils";
import LRU from "lru-cache";
import { LRUCache } from "lru-cache";

type LRUCacheOptions = LRU.SharedOptions<string, any> &
// LRU.SafetyBounds
LRU.LimitedByCount &
LRU.LimitedBySize<string, any> &
LRU.LimitedByTTL & {
/**
* The maximum allowed size for any single item in the cache.
*
* If a larger item is passed to set or returned by a
* fetchMethod, then it will not be stored in the cache.
*/
maxEntrySize?: number; // LRU.LRUSize
sizeCalculation?: LRU.SizeCalculator<string, any>;
};
type LRUCacheOptions = LRUCache.OptionsBase<string, any, any> &
Partial<LRUCache.OptionsMaxLimit<string, any, any>> &
Partial<LRUCache.OptionsSizeLimit<string, any, any>> &
Partial<LRUCache.OptionsTTLLimit<string, any, any>>;

export interface LRUDriverOptions extends Partial<LRUCacheOptions> {}
export interface LRUDriverOptions extends LRUCacheOptions {}

export default defineDriver((opts: LRUDriverOptions = {}) => {
const cache = new LRU({
const cache = new LRUCache({
max: 1000,
sizeCalculation:
opts.maxSize || opts.maxEntrySize
? (value, key) => {
? (value, key: string) => {
return key.length + byteLength(value);
}
: undefined,
Expand Down

0 comments on commit 5b8fc62

Please sign in to comment.