@peerbit/cache
Advanced tools
Comparing version 2.0.6 to 2.1.0-171d517
116
package.json
{ | ||
"name": "@peerbit/cache", | ||
"version": "2.0.6", | ||
"description": "Simple cache", | ||
"type": "module", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"lib", | ||
"src", | ||
"!src/**/__tests__", | ||
"!lib/**/__tests__", | ||
"LICENSE" | ||
], | ||
"module": "lib/esm/index.js", | ||
"types": "lib/esm/index.d.ts", | ||
"exports": { | ||
"import": "./lib/esm/index.js", | ||
"require": "./lib/cjs/index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dao-xyz/peerbit" | ||
}, | ||
"homepage": "https://github.com/dao-xyz/peerbit", | ||
"bugs": "https://github.com/dao-xyz/peerbit/issues", | ||
"scripts": { | ||
"clean": "shx rm -rf lib/*", | ||
"build": "yarn clean && tsc -p tsconfig.json", | ||
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit", | ||
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit", | ||
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@peerbit/time": "2.0.6", | ||
"@types/yallist": "^4.0.1" | ||
}, | ||
"dependencies": { | ||
"yallist": "^4.0.0" | ||
}, | ||
"localMaintainers": [ | ||
"dao.xyz" | ||
], | ||
"gitHead": "e50907578b203c2f16199e91545c5213cf8cdc3e" | ||
"name": "@peerbit/cache", | ||
"version": "2.1.0-171d517", | ||
"description": "Simple cache", | ||
"type": "module", | ||
"types": "./dist/src/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
], | ||
"src/*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"src", | ||
"dist", | ||
"!dist/test", | ||
"!**/*.tsbuildinfo" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": "peerbit", | ||
"parserOptions": { | ||
"project": true, | ||
"sourceType": "module" | ||
}, | ||
"ignorePatterns": [ | ||
"!.aegir.js", | ||
"test/ts-use", | ||
"*.d.ts" | ||
] | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dao-xyz/peerbit" | ||
}, | ||
"homepage": "https://github.com/dao-xyz/peerbit", | ||
"bugs": "https://github.com/dao-xyz/peerbit/issues", | ||
"scripts": { | ||
"clean": "aegir clean", | ||
"build": "aegir build --no-bundle", | ||
"test": "aegir test", | ||
"lint": "aegir lint" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@peerbit/time": "2.0.7-171d517", | ||
"@types/yallist": "^4.0.4" | ||
}, | ||
"dependencies": { | ||
"yallist": "^4.0.0" | ||
}, | ||
"localMaintainers": [ | ||
"dao.xyz" | ||
] | ||
} |
// Fifo | ||
import yallist from "yallist"; | ||
export type CacheData<T> = { value?: T | null; time: number; size: number }; | ||
export interface CacheData<T> { | ||
value?: T | null; | ||
time: number; | ||
size: number; | ||
} | ||
type Key = string | bigint | number; | ||
export class Cache<T = undefined> { | ||
private _map: Map<string, CacheData<T>>; | ||
private deleted: Set<string>; | ||
private list: yallist<string>; | ||
private _map: Map<Key, CacheData<T>>; | ||
private deleted: Set<Key>; | ||
private list: yallist<Key>; | ||
currentSize: number; | ||
@@ -22,3 +27,4 @@ deletedSize: number; | ||
} | ||
has(key: string) { | ||
has(key: Key) { | ||
this.trim(); | ||
@@ -31,7 +37,7 @@ if (this.deleted.has(key)) { | ||
get map(): Map<string, CacheData<T>> { | ||
get map(): Map<Key, CacheData<T>> { | ||
return this._map; | ||
} | ||
get(key: string): T | null | undefined { | ||
get(key: Key): T | null | undefined { | ||
this.trim(); | ||
@@ -47,6 +53,8 @@ if (this.deleted.has(key)) { | ||
const headKey = this.list.head; | ||
if (headKey?.value !== undefined) { | ||
const cacheValue = this._map.get(headKey.value)!; | ||
const outOfDate = | ||
this.ttl !== undefined && cacheValue.time < time - this.ttl; | ||
if (headKey?.value != null) { | ||
const cacheValue = this._map.get(headKey.value); | ||
if (!cacheValue) { | ||
throw new Error("Cache value not found"); | ||
} | ||
const outOfDate = this.ttl != null && cacheValue.time < time - this.ttl; | ||
if (outOfDate || this.currentSize > this.max) { | ||
@@ -68,4 +76,4 @@ this.list.shift(); | ||
del(key: string) { | ||
const cacheValue = this._map.get(key)!; | ||
del(key: Key) { | ||
const cacheValue = this._map.get(key); | ||
if (cacheValue && !this.deleted.has(key)) { | ||
@@ -79,3 +87,3 @@ this.deleted.add(key); | ||
add(key: string, value?: T, size = 1) { | ||
add(key: Key, value?: T, size = 1) { | ||
this.deleted.delete(key); | ||
@@ -90,2 +98,3 @@ const time = +new Date(); | ||
} | ||
clear() { | ||
@@ -92,0 +101,0 @@ this.list = yallist.create(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11720
7
212
126
28
45
114
1
1