@peerbit/cache
Advanced tools
Comparing version 2.0.6-aa577a5 to 2.0.6-cccc078
@@ -1,6 +0,6 @@ | ||
export type CacheData<T> = { | ||
export interface CacheData<T> { | ||
value?: T | null; | ||
time: number; | ||
size: number; | ||
}; | ||
} | ||
type Key = string | bigint | number; | ||
@@ -7,0 +7,0 @@ export declare class Cache<T = undefined> { |
@@ -39,5 +39,8 @@ // Fifo | ||
const headKey = this.list.head; | ||
if (headKey?.value !== undefined) { | ||
if (headKey?.value != null) { | ||
const cacheValue = this._map.get(headKey.value); | ||
const outOfDate = this.ttl !== undefined && cacheValue.time < time - this.ttl; | ||
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) { | ||
@@ -44,0 +47,0 @@ this.list.shift(); |
{ | ||
"name": "@peerbit/cache", | ||
"version": "2.0.6-aa577a5", | ||
"version": "2.0.6-cccc078", | ||
"description": "Simple cache", | ||
@@ -36,3 +36,3 @@ "type": "module", | ||
"eslintConfig": { | ||
"extends": "ipfs", | ||
"extends": "peerbit", | ||
"parserOptions": { | ||
@@ -60,7 +60,8 @@ "project": true, | ||
"build": "aegir build --no-bundle", | ||
"test": "aegir test" | ||
"test": "aegir test", | ||
"lint": "aegir lint" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@peerbit/time": "2.0.6-aa577a5", | ||
"@peerbit/time": "2.0.6-cccc078", | ||
"@types/yallist": "^4.0.4" | ||
@@ -67,0 +68,0 @@ }, |
// 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; | ||
@@ -23,2 +27,3 @@ export class Cache<T = undefined> { | ||
} | ||
has(key: Key) { | ||
@@ -47,6 +52,8 @@ this.trim(); | ||
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) { | ||
@@ -69,3 +76,3 @@ this.list.shift(); | ||
del(key: Key) { | ||
const cacheValue = this._map.get(key)!; | ||
const cacheValue = this._map.get(key); | ||
if (cacheValue && !this.deleted.has(key)) { | ||
@@ -89,2 +96,3 @@ this.deleted.add(key); | ||
} | ||
clear() { | ||
@@ -91,0 +99,0 @@ this.list = yallist.create(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11720
212