New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@avanio/expire-cache

Package Overview
Dependencies
Maintainers
0
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@avanio/expire-cache - npm Package Compare versions

Comparing version 0.6.3 to 0.6.5

52

dist/index.d.ts

@@ -1,4 +0,4 @@

import { CacheEventsMap, ICache as ICache$1 } from '@luolapeikko/cache-types';
import { EventEmitter } from 'events';
import { LogMapping, ISetLogMapping, ILoggerLike } from '@avanio/logger-like';
import { EventEmitter } from 'events';
import { CacheEventsMap, ICacheWithEvents } from '@luolapeikko/cache-types';

@@ -226,5 +226,5 @@ /**

* @template Key - (optional) The type of the cache key (default is string)
* @since v0.6.0
* @since v0.6.5
*/
declare class ExpireCache<Payload, Key = string> extends EventEmitter<CacheEventsMap<Payload, Key>> implements ICache$1<Payload, Key>, ISetLogMapping<ExpireCacheLogMapType> {
declare class ExpireCache<Payload, Key = string> extends EventEmitter<CacheEventsMap<Payload, Key>> implements ICacheWithEvents<Payload, Key>, ISetLogMapping<ExpireCacheLogMapType> {
private readonly cache;

@@ -294,5 +294,5 @@ private readonly cacheTtl;

* @template Key - (optional) The type of the cache key (default is string)
* @since v0.6.0
* @since v0.6.5
*/
declare class ExpireTimeoutCache<Payload, Key = string> extends EventEmitter<CacheEventsMap<Payload, Key>> implements ICache$1<Payload, Key>, ISetLogMapping<ExpireTimeoutCacheLogMapType> {
declare class ExpireTimeoutCache<Payload, Key = string> extends EventEmitter<CacheEventsMap<Payload, Key>> implements ICacheWithEvents<Payload, Key>, ISetLogMapping<ExpireTimeoutCacheLogMapType> {
private readonly cache;

@@ -342,8 +342,13 @@ private readonly cacheTimeout;

};
type TierStatusRecord<T extends TierType<unknown, string>[]> = Record<T[number]['tier'], number>;
type TierStatusInitialRecord<T extends TierType<unknown, string>[]> = Record<T[number]['tier'], 0>;
type TieredCacheStatus<T extends TierType<unknown, string>[]> = {
size: number;
tiers: Record<T[number]['tier'], number>;
tiers: TierStatusRecord<T>;
};
type MultiTierCacheEvents<T extends TierType<unknown, string>[]> = {
update: [status: TieredCacheStatus<T>];
type MultiTierCacheEvents<T extends TierType<unknown, string>[], Key> = {
update: [status: Readonly<TieredCacheStatus<T>>];
set: [Iterable<Key>];
delete: [Iterable<Key>];
clear: [];
};

@@ -367,3 +372,3 @@ declare const defaultLogMap: {

*/
declare abstract class TieredCache<Tiers extends TierType<unknown, string>[], TimeoutEnum extends number, Key> extends EventEmitter<MultiTierCacheEvents<Tiers>> implements ISetLogMapping<TieredCacheLogMapType> {
declare abstract class TieredCache<Tiers extends TierType<unknown, string>[], TimeoutEnum extends number, Key> extends EventEmitter<MultiTierCacheEvents<Tiers, Key>> implements ISetLogMapping<TieredCacheLogMapType> {
abstract readonly cacheName: string;

@@ -373,2 +378,3 @@ protected readonly cache: Map<Key, Tiers[number]>;

private readonly logger;
private statusData;
constructor(logger?: ILoggerLike, logMapping?: Partial<ExpireCacheLogMapType>);

@@ -393,2 +399,9 @@ private logCacheName;

/**
* Set multiple cache entries
* @param tier - cache tier
* @param entries - iterable of key, data pairs
* @param timeout - optional timeout for cache entry. Else timeout will be checked from handleTimeoutValue or default timeout for tier.
*/
setEntries<T extends Tiers[number]>(tier: T['tier'], entries: Iterable<[Key, T['data']]>, timeout?: TimeoutEnum): Promise<void>;
/**
* Get tier type for current key

@@ -408,3 +421,4 @@ */

delete(key: Key): boolean;
status(): TieredCacheStatus<Tiers>;
deleteKeys(keys: Iterable<Key>): number;
status(): Readonly<TieredCacheStatus<Tiers>>;
setLogger(logger: ILoggerLike | undefined): void;

@@ -417,5 +431,5 @@ setLogMapping(logMapping: Partial<ExpireCacheLogMapType>): void;

/**
* Typed helper to set cache entry
* Typed helper to set cache entry and timeouts
*/
protected handleSetValue<T extends Tiers[number]>(key: Key, type: T['tier'], data: T['data']): void;
protected handleSetValue<T extends Tiers[number]>(key: Key, tier: T['tier'], data: T['data'], timeout?: TimeoutEnum): Promise<void>;
protected abstract handleCacheEntry<T extends Tiers[number]>(key: Key, tier: T['tier'], cache: Tiers[number] | undefined): Promise<T['data'] | undefined> | T['data'] | undefined;

@@ -439,5 +453,13 @@ /**

protected abstract handleTierDefaultTimeout(type: Tiers[number]['tier']): TimeoutEnum | undefined;
protected abstract buildStatus(): TieredCacheStatus<Tiers>;
/**
* Build initial status data for cache
* @example
* protected getInitialStatusData(): Readonly<TierStatusInitialRecord<DateCacheTiers>> {
* return {model: 0, object: 0, stringValue: 0} as const;
* }
*/
protected abstract getInitialStatusData(): Readonly<TierStatusInitialRecord<Tiers>>;
protected buildStatus(rebuild: boolean): Readonly<TieredCacheStatus<Tiers>>;
}
export { ExpireCache, type ExpireCacheLogMapType, ExpireTimeoutCache, type ExpireTimeoutCacheLogMapType, type GetCacheTier, type IAsyncCache, type IAsyncCacheOnClearCallback, type ICache, type ICacheOnClearCallback, type ICacheOrAsync, type TierType, TieredCache, type TieredCacheLogMapType, type TieredCacheStatus };
export { ExpireCache, type ExpireCacheLogMapType, ExpireTimeoutCache, type ExpireTimeoutCacheLogMapType, type GetCacheTier, type IAsyncCache, type IAsyncCacheOnClearCallback, type ICache, type ICacheOnClearCallback, type ICacheOrAsync, type TierStatusInitialRecord, type TierStatusRecord, type TierType, TieredCache, type TieredCacheLogMapType, type TieredCacheStatus };

@@ -5,3 +5,17 @@ "use strict";

var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __export = (target, all) => {

@@ -22,4 +36,4 @@ for (var name in all)

// src/index.mts
var src_exports = {};
__export(src_exports, {
var index_exports = {};
__export(index_exports, {
ExpireCache: () => ExpireCache,

@@ -29,7 +43,7 @@ ExpireTimeoutCache: () => ExpireTimeoutCache,

});
module.exports = __toCommonJS(src_exports);
module.exports = __toCommonJS(index_exports);
// src/ExpireCache.mts
var import_events = require("events");
var import_logger_like = require("@avanio/logger-like");
var import_events = require("events");
var defaultLogMap = {

@@ -167,4 +181,4 @@ cleanExpired: import_logger_like.LogLevel.None,

// src/ExpireTimeoutCache.mts
var import_events2 = require("events");
var import_logger_like2 = require("@avanio/logger-like");
var import_events2 = require("events");
var defaultLogMap2 = {

@@ -294,4 +308,4 @@ cleanExpired: import_logger_like2.LogLevel.None,

// src/TieredCache.mts
var import_events3 = require("events");
var import_logger_like3 = require("@avanio/logger-like");
var import_events3 = require("events");
var defaultLogMap3 = {

@@ -317,2 +331,3 @@ clear: import_logger_like3.LogLevel.None,

this.handleCacheEntry = this.handleCacheEntry.bind(this);
this.statusData = { size: 0, tiers: __spreadValues({}, this.getInitialStatusData()) };
}

@@ -347,8 +362,25 @@ logCacheName() {

this.logger.logKey("set", `MultiTierCache ${this.cacheName} set: '${String(key)}' tier: ${tier}`);
this.handleSetValue(key, tier, data);
const entryTimeoutValue = await this.handleTimeoutValue(key, tier, data);
this.setTimeout(key, timeout || entryTimeoutValue || this.handleTierDefaultTimeout(tier));
this.emit("update", this.buildStatus());
await this.handleSetValue(key, tier, data, timeout);
this.emit("set", [key]);
this.emit("update", this.buildStatus(true));
}
/**
* Set multiple cache entries
* @param tier - cache tier
* @param entries - iterable of key, data pairs
* @param timeout - optional timeout for cache entry. Else timeout will be checked from handleTimeoutValue or default timeout for tier.
*/
async setEntries(tier, entries, timeout) {
const entriesArray = Array.from(entries);
this.logger.logKey("set", `MultiTierCache ${this.cacheName} setEntries (count: ${entriesArray.length.toString()}) tier: ${tier}`);
for (const [key, data] of entriesArray) {
await this.handleSetValue(key, tier, data, timeout);
}
this.emit(
"set",
entriesArray.map(([key]) => key)
);
this.emit("update", this.buildStatus(true));
}
/**
* Get tier type for current key

@@ -373,5 +405,5 @@ */

if (done) {
return { value: value == null ? void 0 : value[0], done: true };
return { value: void 0, done };
}
return { value: await currentTierResolve(value[0], tier, value[1]), done: false };
return { value: await currentTierResolve(value[0], tier, value[1]), done };
}

@@ -411,6 +443,9 @@ };

clear() {
const keys = new Set(this.cache.keys());
this.clearAllTimeouts();
this.cache.clear();
this.logger.logKey("clear", `MultiTierCache ${this.cacheName} clear`);
this.emit("update", this.buildStatus());
this.emit("delete", keys);
this.emit("clear");
this.emit("update", this.buildStatus(true));
}

@@ -420,8 +455,24 @@ delete(key) {

const isDeleted = this.cache.delete(key);
this.logger.logKey("delete", `MultiTierCache ${this.cacheName} delete: '${String(key)}'`);
this.emit("update", this.buildStatus());
if (isDeleted) {
this.logger.logKey("delete", `MultiTierCache ${this.cacheName} delete: '${String(key)}'`);
this.emit("delete", [key]);
this.emit("update", this.buildStatus(true));
}
return isDeleted;
}
deleteKeys(keys) {
const deleteKeys = [];
for (const key of keys) {
this.clearTimeoutKey(key);
if (this.cache.delete(key)) {
deleteKeys.push(key);
}
}
this.logger.logKey("delete", `MultiTierCache ${this.cacheName} deleteKeys (count: ${deleteKeys.length.toString()})`);
this.emit("delete", deleteKeys);
this.emit("update", this.buildStatus(true));
return deleteKeys.length;
}
status() {
return this.buildStatus();
return this.buildStatus(false);
}

@@ -473,3 +524,3 @@ setLogger(logger) {

}
this.emit("update", this.buildStatus());
this.emit("update", this.buildStatus(true));
} catch (error) {

@@ -480,7 +531,24 @@ this.logger.error(error);

/**
* Typed helper to set cache entry
* Typed helper to set cache entry and timeouts
*/
handleSetValue(key, type, data) {
this.cache.set(key, { tier: type, data });
async handleSetValue(key, tier, data, timeout) {
this.cache.set(key, { tier, data });
this.setTimeout(key, timeout || await this.handleTimeoutValue(key, tier, data));
}
buildStatus(rebuild) {
if (!rebuild) {
return this.statusData;
}
this.statusData = Object.freeze({
size: this.cache.size,
tiers: Array.from(this.cache.values()).reduce(
(acc, { tier: type }) => {
acc[type]++;
return acc;
},
__spreadValues({}, this.getInitialStatusData())
)
});
return this.statusData;
}
};

@@ -487,0 +555,0 @@ // Annotate the CommonJS export names for ESM import in node:

{
"name": "@avanio/expire-cache",
"version": "0.6.3",
"version": "0.6.5",
"description": "Typescript/Javascript cache with expiration",

@@ -59,30 +59,32 @@ "main": "./dist/index.js",

"devDependencies": {
"@avanio/logger-like": "^0.2.7",
"@luolapeikko/cache-types": "^0.0.7",
"@stylistic/eslint-plugin": "^2.10.1",
"@stylistic/eslint-plugin-ts": "^2.10.1",
"@types/node": "^20.17.6",
"@avanio/logger-like": "^0.2.9",
"@luolapeikko/cache-types": "^0.0.8",
"@stylistic/eslint-plugin": "^2.12.1",
"@stylistic/eslint-plugin-ts": "^2.12.1",
"@types/node": "^22.10.5",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^8.13.0",
"@typescript-eslint/parser": "^8.13.0",
"@vitest/coverage-v8": "^2.1.4",
"@vitest/ui": "^2.1.4",
"c8": "^10.1.2",
"@typescript-eslint/eslint-plugin": "^8.19.1",
"@typescript-eslint/parser": "^8.19.1",
"@vitest/coverage-v8": "^2.1.8",
"@vitest/ui": "^2.1.8",
"c8": "^10.1.3",
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-import-resolver-typescript": "^3.7.0",
"eslint-plugin-deprecation": "^3.0.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-sonarjs": "^0.23.0",
"prettier": "^3.3.3",
"prettier": "^3.4.2",
"sinon": "^19.0.2",
"ts-node": "^10.9.2",
"tsup": "^8.3.5",
"typescript": "^5.6.3",
"vite": "^5.4.10",
"vitest": "^2.1.4"
"typescript": "^5.7.3",
"vite": "^6.0.7",
"vitest": "^2.1.8"
},
"peerDependencies": {
"@avanio/logger-like": "^0.2.7",
"@luolapeikko/cache-types": "^0.0"
"@avanio/logger-like": "^0.2.6",
"@luolapeikko/cache-types": "^0.0.8"
},

@@ -89,0 +91,0 @@ "scripts": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc