New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@khalidsheet/cacheable

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@khalidsheet/cacheable - npm Package Compare versions

Comparing version
0.5.0
to
0.5.1
+74
dist/index.d.mts
interface Cacheable {
/**
* Get a value from the cache.
* @param key The key to get the value for.
* @returns The value if it exists and is not expired, otherwise null.
*/
get<T>(key: string): T | null;
/**
* Set a value in the cache.
* @param key The key to set the value for.
* @param value The value to set.
* @param ttl The time-to-live in milliseconds.
* @returns void
*/
set<T>(key: string, value: T, ttl: number): void;
/**
* Check if a key exists in the cache
* @param key - The cache key
* @returns True if the key exists in the cache, false otherwise
*/
has(key: string): boolean;
/**
* Get a value from the cache, or set it if it doesn't exist.
*
* @param key The key to get the value for.
* @param ttl The time-to-live in milliseconds.
* @param callback The callback to get the value if it doesn't exist.
* @returns The value.
*/
remember<T>(key: string, ttl: number, callback: () => T): T;
/**
* Get a value from the cache, or set it if it doesn't exist. The value will be stored indefinitely.
* @param key The key to get the value for.
* @param callback The callback to get the value if it doesn't exist.
*/
rememberForever<T>(key: string, callback: () => T): T;
/**
* Invalidate a value in the cache.
* @param key The key to invalidate.
* @returns void
*/
invalidate(key: string): void;
/**
* Clear the cache.
* @returns void
*/
clear(): void;
}
type CreateCacheOptions = {
cacheStrategy?: Cacheable;
};
declare class CacheFactory {
/**
* Create a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance, defaults to in-memory cache
*/
static createCache(options?: CreateCacheOptions): Cacheable;
/**
* Use a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance
*/
static use(cacheStrategy: Cacheable): Cacheable;
}
interface CacheItem {
value: any;
expiry: number;
}
export { CacheFactory, type CacheItem, type Cacheable };
// src/adapters/InMemoryCacheable.ts
var InMemoryCacheable = class {
_cache = /* @__PURE__ */ new Map();
get(key) {
const item = this._cache.get(key);
if (!item) {
return null;
}
if (item.expiry < Date.now()) {
this._cache.delete(key);
return null;
}
return item.value;
}
set(key, value, ttl) {
if (ttl <= 0) {
this._cache.delete(key);
return;
}
let cachedValue = value;
if (typeof value === "function") {
this._cache.delete(key);
cachedValue = value();
}
this._cache.set(key, {
value: cachedValue,
expiry: this._getTTl(ttl)
});
}
has(key) {
return this._cache.has(key);
}
remember(key, ttl, callback) {
const item = this._cache.get(key);
if (item && item.expiry > Date.now()) {
return item.value;
}
const value = callback();
this._cache.set(key, {
value,
expiry: this._getTTl(ttl)
});
return value;
}
rememberForever(key, callback) {
return this.remember(key, 31104e4, callback);
}
invalidate(key) {
this._cache.delete(key);
}
clear() {
this._cache.clear();
}
_getTTl(ttl) {
return Date.now() + ttl;
}
};
// src/factories/CacheFactory.ts
var CacheFactory = class {
/**
* Create a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance, defaults to in-memory cache
*/
static createCache(options) {
const { cacheStrategy } = options || {};
if (cacheStrategy) {
return cacheStrategy;
}
return new InMemoryCacheable();
}
/**
* Use a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance
*/
static use(cacheStrategy) {
return cacheStrategy;
}
};
export {
CacheFactory
};
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
preset: "ts-jest",
testEnvironment: "node",
collectCoverage: true, // Enable coverage collection
coverageDirectory: "./coverage", // Where to output the coverage report
coverageReporters: ["text", "lcov"], // Types of coverage reports to generate
moduleNameMapper: {
// Ensures TypeScript files are resolved correctly
"^src/(.*)$": "<rootDir>/src/$1",
},
};
+29
-2

@@ -0,1 +1,27 @@

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
CacheFactory: () => CacheFactory
});
module.exports = __toCommonJS(src_exports);
// src/adapters/InMemoryCacheable.ts

@@ -82,4 +108,5 @@ var InMemoryCacheable = class {

};
export {
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CacheFactory
};
});
+5
-4
{
"name": "@khalidsheet/cacheable",
"version": "0.5.0",
"version": "0.5.1",
"description": "A simple in-memory caching solution for Node.js, with a flexible design where you can define your own cache policies.",
"main": "dist/index.js",
"module": "dist/index.mjs",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"private": false,

@@ -37,2 +37,3 @@ "scripts": {

"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"tsup": "^8.3.5",

@@ -39,0 +40,0 @@ "typescript": "^5.6.3"

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
CacheFactory: () => CacheFactory
});
module.exports = __toCommonJS(src_exports);
// src/adapters/InMemoryCacheable.ts
var InMemoryCacheable = class {
_cache = /* @__PURE__ */ new Map();
get(key) {
const item = this._cache.get(key);
if (!item) {
return null;
}
if (item.expiry < Date.now()) {
this._cache.delete(key);
return null;
}
return item.value;
}
set(key, value, ttl) {
if (ttl <= 0) {
this._cache.delete(key);
return;
}
let cachedValue = value;
if (typeof value === "function") {
this._cache.delete(key);
cachedValue = value();
}
this._cache.set(key, {
value: cachedValue,
expiry: this._getTTl(ttl)
});
}
has(key) {
return this._cache.has(key);
}
remember(key, ttl, callback) {
const item = this._cache.get(key);
if (item && item.expiry > Date.now()) {
return item.value;
}
const value = callback();
this._cache.set(key, {
value,
expiry: this._getTTl(ttl)
});
return value;
}
rememberForever(key, callback) {
return this.remember(key, 31104e4, callback);
}
invalidate(key) {
this._cache.delete(key);
}
clear() {
this._cache.clear();
}
_getTTl(ttl) {
return Date.now() + ttl;
}
};
// src/factories/CacheFactory.ts
var CacheFactory = class {
/**
* Create a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance, defaults to in-memory cache
*/
static createCache(options) {
const { cacheStrategy } = options || {};
if (cacheStrategy) {
return cacheStrategy;
}
return new InMemoryCacheable();
}
/**
* Use a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance
*/
static use(cacheStrategy) {
return cacheStrategy;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CacheFactory
});
interface Cacheable {
/**
* Get a value from the cache.
* @param key The key to get the value for.
* @returns The value if it exists and is not expired, otherwise null.
*/
get<T>(key: string): T | null;
/**
* Set a value in the cache.
* @param key The key to set the value for.
* @param value The value to set.
* @param ttl The time-to-live in milliseconds.
* @returns void
*/
set<T>(key: string, value: T, ttl: number): void;
/**
* Check if a key exists in the cache
* @param key - The cache key
* @returns True if the key exists in the cache, false otherwise
*/
has(key: string): boolean;
/**
* Get a value from the cache, or set it if it doesn't exist.
*
* @param key The key to get the value for.
* @param ttl The time-to-live in milliseconds.
* @param callback The callback to get the value if it doesn't exist.
* @returns The value.
*/
remember<T>(key: string, ttl: number, callback: () => T): T;
/**
* Get a value from the cache, or set it if it doesn't exist. The value will be stored indefinitely.
* @param key The key to get the value for.
* @param callback The callback to get the value if it doesn't exist.
*/
rememberForever<T>(key: string, callback: () => T): T;
/**
* Invalidate a value in the cache.
* @param key The key to invalidate.
* @returns void
*/
invalidate(key: string): void;
/**
* Clear the cache.
* @returns void
*/
clear(): void;
}
type CreateCacheOptions = {
cacheStrategy?: Cacheable;
};
declare class CacheFactory {
/**
* Create a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance, defaults to in-memory cache
*/
static createCache(options?: CreateCacheOptions): Cacheable;
/**
* Use a cache instance
* @param cacheStrategy The cache strategy to use
* @returns The cache instance
*/
static use(cacheStrategy: Cacheable): Cacheable;
}
interface CacheItem {
value: any;
expiry: number;
}
export { CacheFactory, type CacheItem, type Cacheable };