Launch Week Day 3: Introducing Organization Notifications in Socket.Learn More
Socket
Book a DemoSign in
Socket

simple-in-memory-cache

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-in-memory-cache - npm Package Compare versions

Comparing version
0.3.0
to
0.3.1
+1
dist/cache.test.d.ts
export {};
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const cache_1 = require("./cache");
jest.setTimeout(30 * 1000); // give up to 60 seconds, since we deal with timeouts that we want to test on the ~15 second range
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
describe('cache', () => {
it('should be able to add an item to the cache', () => {
const { set } = (0, cache_1.createCache)();
set('meaning of life', 42);
});
it('should be able to get an item from the cache', () => {
const { set, get } = (0, cache_1.createCache)();
set('how many licks does it take to get to the center of a tootsie pop?', 3);
const licks = get('how many licks does it take to get to the center of a tootsie pop?');
expect(licks).toEqual(3);
});
it('should respect the default expiration for the cache', () => __awaiter(void 0, void 0, void 0, function* () {
const { set, get } = (0, cache_1.createCache)({ defaultSecondsUntilExpiration: 10 }); // we're gonna use this cache to keep track of the popcorn in the microwave - we should check more regularly since it changes quickly!
set('how popped is the popcorn?', 'not popped');
// prove that we recorded the value and its accessible immediately after setting
const popcornStatus = get('how popped is the popcorn?');
expect(popcornStatus).toEqual('not popped');
// prove that the value is still accessible after 9 seconds, since default ttl is 10 seconds
yield sleep(9 * 1000);
const popcornStatusAfter9Sec = get('how popped is the popcorn?');
expect(popcornStatusAfter9Sec).toEqual('not popped'); // still should say not popped
// and prove that after a total of 9 seconds, the status is no longer in the cache
yield sleep(1 * 1000); // sleep 1 more second
const popcornStatusAfter10Sec = get('how popped is the popcorn?');
expect(popcornStatusAfter10Sec).toEqual(undefined); // no longer defined, since the default seconds until expiration was 15
}));
it('should respect the default expiration for the cache set by shorthand alias', () => __awaiter(void 0, void 0, void 0, function* () {
const { set, get } = (0, cache_1.createCache)({ seconds: 10 }); // we're gonna use this cache to keep track of the popcorn in the microwave - we should check more regularly since it changes quickly!
set('how popped is the popcorn?', 'not popped');
// prove that we recorded the value and its accessible immediately after setting
const popcornStatus = get('how popped is the popcorn?');
expect(popcornStatus).toEqual('not popped');
// prove that the value is still accessible after 9 seconds, since default ttl is 10 seconds
yield sleep(9 * 1000);
const popcornStatusAfter9Sec = get('how popped is the popcorn?');
expect(popcornStatusAfter9Sec).toEqual('not popped'); // still should say not popped
// and prove that after a total of 9 seconds, the status is no longer in the cache
yield sleep(1 * 1000); // sleep 1 more second
const popcornStatusAfter10Sec = get('how popped is the popcorn?');
expect(popcornStatusAfter10Sec).toEqual(undefined); // no longer defined, since the default seconds until expiration was 15
}));
it('should respect the item level expiration for the cache', () => __awaiter(void 0, void 0, void 0, function* () {
const { set, get } = (0, cache_1.createCache)(); // remember, default expiration is greater than 1 min
set('ice cream state', 'solid', { secondsUntilExpiration: 5 }); // ice cream changes quickly in the heat! lets keep a quick eye on this
// prove that we recorded the value and its accessible immediately after setting
const iceCreamState = get('ice cream state');
expect(iceCreamState).toEqual('solid');
// prove that the value is still accessible after 4 seconds, since default ttl is 5 seconds
yield sleep(4 * 1000);
const iceCreamStateAfter4Sec = get('ice cream state');
expect(iceCreamStateAfter4Sec).toEqual('solid'); // still should say solid
// and prove that after a total of 5 seconds, the state is no longer in the cache
yield sleep(1 * 1000); // sleep 1 more second
const iceCreamStateAfter5Sec = get('ice cream state');
expect(iceCreamStateAfter5Sec).toEqual(undefined); // no longer defined, since the item level seconds until expiration was 5
}));
it('should accurately get keys', () => {
// create the cache
const { set, keys } = (0, cache_1.createCache)();
// check key is added when value is set
set('meaning-of-life', '42');
const keys1 = keys();
expect(keys1.length).toEqual(1);
expect(keys1[0]).toEqual('meaning-of-life');
// check that there are no duplicates when key value is updated
set('meaning-of-life', '42.0');
const keys2 = keys();
expect(keys2.length).toEqual(1);
expect(keys2[0]).toEqual('meaning-of-life');
// check that multiple keys can be set
set('purpose-of-life', 'propagation');
const keys3 = keys();
expect(keys3.length).toEqual(2);
expect(keys3[1]).toEqual('purpose-of-life');
// check that invalidation removes the key
set('meaning-of-life', undefined);
const keys4 = keys();
expect(keys4.length).toEqual(1);
expect(keys4[0]).toEqual('purpose-of-life');
});
});
//# sourceMappingURL=cache.test.js.map
{"version":3,"file":"cache.test.js","sourceRoot":"","sources":["../src/cache.test.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mCAAsC;AAEtC,IAAI,CAAC,UAAU,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,kGAAkG;AAE9H,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAEhF,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACrB,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QACpD,MAAM,EAAE,GAAG,EAAE,GAAG,IAAA,mBAAW,GAAE,CAAC;QAC9B,GAAG,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,8CAA8C,EAAE,GAAG,EAAE;QACtD,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAA,mBAAW,GAAE,CAAC;QACnC,GAAG,CACD,oEAAoE,EACpE,CAAC,CACF,CAAC;QACF,MAAM,KAAK,GAAG,GAAG,CACf,oEAAoE,CAC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AC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAChC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
# simple-in-memory-cache
![test](https://github.com/ehmpathy/simple-in-memory-cache/workflows/test/badge.svg)
![publish](https://github.com/ehmpathy/simple-in-memory-cache/workflows/publish/badge.svg)
A simple in-memory cache, for nodejs and the browser, with time based expiration policies.
# Install
```sh
npm install --save simple-in-memory-cache
```
# Example
Quickly set and get from the cache:
```ts
import { createCache } from 'simple-in-memory-cache';
const { set, get } = createCache();
set('meaning of life', 42);
const meaningOfLife = get('meaning of life'); // returns 42
```
Items in the cache live 5 minutes until expiration, by default.
You can change this default when creating the cache:
```ts
const { set, get } = createCache({ defaultSecondsUntilExpiration: 10 * 60 }); // updates the default seconds until expiration to 10 minutes
```
And you can also override this when setting an item:
```ts
set('acceleration due to gravity', 9.81, { secondsUntilExpiration: Infinity }); // gravity will not change, so we dont need to expire it
```
+11
-1

@@ -14,4 +14,14 @@ export interface SimpleInMemoryCache<T> {

}
export declare const createCache: <T>({ defaultSecondsUntilExpiration }?: {
export declare const createCache: <T>({ seconds, defaultSecondsUntilExpiration: defaultSecondsUntilExpirationInput, }?: {
/**
* the number of seconds items in the cache expire after
*/
defaultSecondsUntilExpiration?: number | undefined;
/**
* a shorthand alias for `defaultSecondsUntilExpiration`
*
* note
* - if both options are set, `defaultSecondsUntilExpirationInput` takes precedence
*/
seconds?: number | undefined;
}) => SimpleInMemoryCache<T>;
+6
-2

@@ -5,7 +5,10 @@ "use strict";

const getMseNow = () => new Date().getTime();
exports.createCache = ({ defaultSecondsUntilExpiration = 5 * 60 } = {}) => {
const createCache = ({ seconds, defaultSecondsUntilExpiration: defaultSecondsUntilExpirationInput, } = {}) => {
var _a;
// resolve input alias
const defaultSecondsUntilExpiration = (_a = defaultSecondsUntilExpirationInput !== null && defaultSecondsUntilExpirationInput !== void 0 ? defaultSecondsUntilExpirationInput : seconds) !== null && _a !== void 0 ? _a : 5 * 60;
// initialize a fresh in-memory cache object
const cache = {};
// define how to set an item into the cache
const set = (key, value, { secondsUntilExpiration = defaultSecondsUntilExpiration } = {}) => {
const set = (key, value, { secondsUntilExpiration = defaultSecondsUntilExpiration, } = {}) => {
// handle cache invalidation

@@ -36,2 +39,3 @@ if (value === undefined) {

};
exports.createCache = createCache;
//# sourceMappingURL=cache.js.map

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

{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":";;;AAUA,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;AAEhC,QAAA,WAAW,GAAG,CAAI,EAAE,6BAA6B,GAAG,CAAC,GAAG,EAAE,KAAiD,EAAE,EAExH,EAAE;IACF,4CAA4C;IAC5C,MAAM,KAAK,GAAgC,EAAE,CAAC;IAE9C,2CAA2C;IAC3C,MAAM,GAAG,GAAG,CACV,GAAW,EACX,KAAoB,EACpB,EAAE,sBAAsB,GAAG,6BAA6B,KAA0C,EAAE,EACpG,EAAE;QACF,4BAA4B;QAC5B,IAAI,KAAK,KAAK,SAAS,EAAE;YACvB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO;SACR;QAED,iBAAiB;QACjB,MAAM,YAAY,GAAG,SAAS,EAAE,GAAG,sBAAsB,GAAG,IAAI,CAAC;QACjE,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;IACvC,CAAC,CAAC;IAEF,2CAA2C;IAC3C,MAAM,GAAG,GAAG,CAAC,GAAW,EAAE,EAAE;QAC1B,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,CAAC,YAAY;YAAE,OAAO,SAAS,CAAC,CAAC,kCAAkC;QACvE,IAAI,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AE,GAAG,sBAAsB,GAAG,IAAI,CAAC;QACjE,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;IACvC,CAAC,CAAC;IAEF,2CAA2C;IAC3C,MAAM,GAAG,GAAG,CAAC,GAAW,EAAE,EAAE;QAC1B,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,CAAC,YAAY;YAAE,OAAO,SAAS,CAAC,CAAC,kCAAkC;QACvE,IAAI,YAAY,CAAC,YAAY,GAAG,SAAS,EAAE;YAAE,OAAO,SAAS,CAAC,CAAC,qCAAqC;QACpG,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC,mEAAmE;IAChG,CAAC,CAAC;IAEF,oCAAoC;IACpC,MAAM,IAAI,GAAG,GAAG,EAAE,CAChB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;SAClB,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,YAAY,GAAG,SAAS,EAAE,CAAC;SACxD,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC;IAEzB,iBAAiB;IACjB,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;AAC5B,CAAC,CAAC;AA3DW,QAAA,WAAW,eA2DtB"}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCache = void 0;
var cache_1 = require("./cache");
Object.defineProperty(exports, "createCache", { enumerable: true, get: function () { return cache_1.createCache; } });
//# sourceMappingURL=index.js.map

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

{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,iCAA2D;AAAlD,oGAAA,WAAW,OAAA"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iCAA2D;AAAlD,oGAAA,WAAW,OAAA"}
{
"name": "simple-in-memory-cache",
"version": "0.3.0",
"author": "ehmpathy",
"description": "A simple in-memory cache, for nodejs and the browser, with time based expiration policies.",
"author": "UladKasach @uladkasach",
"license": "MIT",
"repository": "uladkasach/simple-in-memory-cache",
"version": "0.3.1",
"repository": "ehmpathy/simple-in-memory-cache",
"homepage": "https://github.com/ehmpathy/simple-in-memory-cache",

@@ -32,34 +31,57 @@ "keywords": [

"scripts": {
"build:clean": "rm -rf ./dist",
"build:ts": "tsc -p ./tsconfig.build.json",
"build": "npm run build:clean && npm run build:ts",
"test:types": "tsc --noEmit",
"test:format": "prettier --parser typescript --check 'src/**/*.ts' --config ./prettier.config.js",
"test:lint": "eslint -c ./.eslintrc.js src/**/*.ts",
"test:unit": "jest --forceExit --verbose --passWithNoTests",
"test:integration": "jest -c ./jest.integration.config.js --forceExit --verbose --passWithNoTests",
"test": "npm run test:types && npm run test:lint && npm run test:unit && npm run test:integration",
"commit:with-cli": "npx cz",
"fix:format:prettier": "prettier --write '**/*.ts' --config ./prettier.config.js",
"fix:format": "npm run fix:format:prettier",
"fix:lint": "eslint -c ./.eslintrc.js src/**/*.ts --fix",
"build:clean": "rm dist/ -rf",
"build:compile": "tsc -p ./tsconfig.build.json",
"build": "npm run build:clean && npm run build:compile",
"test:commits": "LAST_TAG=$(git describe --tags --abbrev=0 @^ 2> /dev/null || git rev-list --max-parents=0 HEAD) && npx commitlint --from $LAST_TAG --to HEAD --verbose",
"test:types": "tsc -p ./tsconfig.build.json --noEmit",
"test:format:prettier": "prettier --parser typescript --check 'src/**/*.ts' --config ./prettier.config.js",
"test:format": "npm run test:format:prettier",
"test:lint:deps": "npx depcheck -c ./depcheckrc.yml",
"test:lint:eslint": "eslint -c ./.eslintrc.js src/**/*.ts",
"test:lint": "npm run test:lint:eslint && npm run test:lint:deps",
"test:unit": "jest -c ./jest.unit.config.ts --forceExit --verbose --passWithNoTests",
"test:integration": "jest -c ./jest.integration.config.ts --forceExit --verbose --passWithNoTests",
"test:acceptance:locally": "npm run build && LOCALLY=true jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests",
"test": "npm run test:commits && npm run test:types && npm run test:format && npm run test:lint && npm run test:unit && npm run test:integration && npm run test:acceptance:locally",
"test:acceptance": "npm run build && jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests",
"prepush": "npm run test && npm run build",
"prepublish": "npm run build",
"preversion": "npm run prepublish && npm run test",
"postversion": "git push origin master --tags --no-verify"
"preversion": "npm run prepush",
"postversion": "git push origin HEAD --tags --no-verify"
},
"devDependencies": {
"@types/jest": "^24.0.18",
"@types/uuid": "^3.4.5",
"@typescript-eslint/eslint-plugin": "2.19.0",
"@typescript-eslint/parser": "2.19.0",
"eslint": "6.1.0",
"eslint-config-airbnb-base": "14.0.0",
"eslint-config-airbnb-typescript": "7.0.0",
"eslint-config-prettier": "6.10.0",
"eslint-plugin-import": "2.20.1",
"eslint-plugin-prettier": "3.1.2",
"husky": "^1.3.1",
"jest": "^25.5.4",
"prettier": "^2.0.4",
"ts-jest": "^25.4.0",
"typescript": "^3.8.3",
"uuid": "^3.3.3"
"@commitlint/config-conventional": "13.1.0",
"@trivago/prettier-plugin-sort-imports": "2.0.4",
"@tsconfig/node-lts-strictest": "18.12.1",
"@types/jest": "29.2.4",
"@typescript-eslint/eslint-plugin": "5.46.1",
"@typescript-eslint/parser": "5.46.1",
"commitlint": "^17.6.7",
"core-js": "3.26.1",
"cz-conventional-changelog": "3.3.0",
"declapract": "^0.11.2",
"declapract-typescript-ehmpathy": "^0.25.1",
"depcheck": "1.4.3",
"eslint": "8.30.0",
"eslint-config-airbnb-typescript": "17.0.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-prettier": "4.2.1",
"husky": "7.0.2",
"jest": "29.3.1",
"prettier": "2.8.1",
"ts-jest": "29.0.3",
"ts-node": "10.9.1",
"typescript": "4.9.4"
},
"dependencies": {}
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
# Changelog
## [0.3.0](https://www.github.com/ehmpathy/simple-in-memory-cache/compare/v0.2.1...v0.3.0) (2022-11-24)
### Features
* **keys:** enable accurate retrieval of all valid cache keys ([5a70816](https://www.github.com/ehmpathy/simple-in-memory-cache/commit/5a708160850be4460f421ef39573ea54da17fbb2))
### [0.2.1](https://www.github.com/ehmpathy/simple-in-memory-cache/compare/v0.2.0...v0.2.1) (2022-11-23)
### Bug Fixes
* **cicd:** add test:format script to unblock ghactions ([f4f645c](https://www.github.com/ehmpathy/simple-in-memory-cache/commit/f4f645ce6ec9d44ea44a2f175f5fbcdddbacd45f))
## [0.2.0](https://www.github.com/ehmpathy/simple-in-memory-cache/compare/v0.1.0...v0.2.0) (2022-11-23)
### Features
* **cicd:** add please release github cicd ([43f27ea](https://www.github.com/ehmpathy/simple-in-memory-cache/commit/43f27ea69477e5e3966b2eec3289f2f4d0d99dca))
### Bug Fixes
* **remote:** replace references from uladkasach repo to ehmpathy repo ([983e8ac](https://www.github.com/ehmpathy/simple-in-memory-cache/commit/983e8ac3ae9eeb68cc6d63505d6c8ed851f69d57))
* **types:** expose a type for an instance of the cache ([1bcbf9b](https://www.github.com/ehmpathy/simple-in-memory-cache/commit/1bcbf9b9aa38934ce08f86e628938681d1dfd2ee))
# simple-in-memory-cache
![ci_on_commit](https://github.com/ehmpathy/simple-in-memory-cache/workflows/ci_on_commit/badge.svg)
![deploy_on_tag](https://github.com/ehmpathy/simple-in-memory-cache/workflows/deploy_on_tag/badge.svg)
A simple in-memory cache, for nodejs and the browser, with time based expiration policies.
# Install
```sh
npm install --save simple-in-memory-cache
```
# Example
Quickly set and get from the cache:
```ts
import { createCache } from 'simple-in-memory-cache';
const { set, get } = createCache();
set('meaning of life', 42);
const meaningOfLife = get('meaning of life'); // returns 42
```
Items in the cache live 5 minutes until expiration, by default.
You can change this default when creating the cache:
```ts
const { set, get } = createCache({ defaultSecondsUntilExpiration: 10 * 60 }); // updates the default seconds until expiration to 10 minutes
```
And you can also override this when setting an item:
```ts
set('acceleration due to gravity', 9.81, { secondsUntilExpiration: Infinity }); // gravity will not change, so we dont need to expire it
```