Socket
Socket
Sign inDemoInstall

@platform/cache

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@platform/cache - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

8

lib/MemoryCache/MemoryCache.d.ts

@@ -12,7 +12,11 @@ import * as t from '../types';

exists(key: K): boolean;
get<V>(key: K, defaultValue?: () => V): V;
get<V>(key: K, args?: t.MemoryCacheGetValue<V> | t.IMemoryCacheGetOptions<V>): V;
getAsync<V>(key: K, args?: t.MemoryCacheGetValue<V> | t.IMemoryCacheGetOptions<V>): Promise<V>;
put<V>(key: K, value: V): this;
delete(key: K): this;
clear(): this;
clear(args?: {
filter?: t.MemoryCacheFilter;
}): this;
private item;
private getItem;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var rxjs_1 = require("rxjs");

@@ -24,10 +25,35 @@ var operators_1 = require("rxjs/operators");

};
MemoryCache.prototype.get = function (key, defaultValue) {
var value = this.item(key).value;
if (value === undefined && typeof defaultValue === 'function') {
value = defaultValue();
this.put(key, value);
MemoryCache.prototype.get = function (key, args) {
var res = this.getItem(key, args);
if (res.retrieved) {
this.put(key, res.value);
}
return value;
return res.value;
};
MemoryCache.prototype.getAsync = function (key, args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var res, _a, _b, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
res = this.getItem(key, args);
if (!res.retrieved) return [3, 4];
_a = this.put;
_b = [key];
if (!isPromise(res.value)) return [3, 2];
return [4, res.value];
case 1:
_c = _d.sent();
return [3, 3];
case 2:
_c = res.value;
_d.label = 3;
case 3:
_a.apply(this, _b.concat([_c]));
_d.label = 4;
case 4: return [2, res.value];
}
});
});
};
MemoryCache.prototype.put = function (key, value) {

@@ -49,4 +75,14 @@ var _this = this;

};
MemoryCache.prototype.clear = function () {
this.values = {};
MemoryCache.prototype.clear = function (args) {
var _this = this;
if (args === void 0) { args = {}; }
var filter = args.filter;
if (filter) {
Object.keys(this.values)
.filter(filter)
.forEach(function (key) { return delete _this.values[key]; });
}
else {
this.values = {};
}
return this;

@@ -60,4 +96,17 @@ };

};
MemoryCache.prototype.getItem = function (key, args) {
var getValue = typeof args === 'function' ? args : args ? args.getValue : undefined;
var force = typeof args === 'object' ? args.force : false;
var value = this.item(key).value;
var retrieved = typeof getValue === 'function' && (force || value === undefined);
if (retrieved && typeof getValue === 'function') {
value = getValue();
}
return { value: value, retrieved: retrieved };
};
return MemoryCache;
}());
exports.MemoryCache = MemoryCache;
function isPromise(value) {
return typeof value === 'object' && typeof value.then === 'function';
}

@@ -24,3 +24,3 @@ "use strict";

});
it('caches a default value', function () {
it('caches a default value (via func)', function () {
var cache = MemoryCache_1.MemoryCache.create();

@@ -34,2 +34,53 @@ var res1 = cache.get('FOO');

});
it('caches a default value (via args { func })', function () {
var cache = MemoryCache_1.MemoryCache.create();
var res1 = cache.get('FOO');
var res2 = cache.get('FOO', { getValue: function () { return 123; } });
var res3 = cache.get('FOO');
chai_1.expect(res1).to.eql(undefined);
chai_1.expect(res2).to.eql(123);
chai_1.expect(res3).to.eql(123);
});
it('cached default value (getAsync)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var cache, res1, res2, res3, res4;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
cache = MemoryCache_1.MemoryCache.create();
return [4, cache.getAsync('FOO', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, 123];
}); }); })];
case 1:
res1 = _a.sent();
return [4, cache.getAsync('BAR', { getValue: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, 456];
}); }); } })];
case 2:
res2 = _a.sent();
chai_1.expect(res1).to.eql(123);
chai_1.expect(res2).to.eql(456);
res3 = cache.get('FOO');
res4 = cache.get('BAR');
chai_1.expect(res3).to.eql(123);
chai_1.expect(res4).to.eql(456);
return [2];
}
});
}); });
it('forces new retrieval of value (via args { force })', function () {
var count = 0;
var getValue = function () {
count++;
return count;
};
var cache = MemoryCache_1.MemoryCache.create();
chai_1.expect(cache.get('FOO', { getValue: getValue })).to.eql(1);
chai_1.expect(cache.get('FOO', { getValue: getValue })).to.eql(1);
chai_1.expect(cache.get('FOO', { getValue: getValue })).to.eql(1);
chai_1.expect(count).to.eql(1);
chai_1.expect(cache.get('FOO', { getValue: getValue, force: true })).to.eql(2);
chai_1.expect(count).to.eql(2);
chai_1.expect(cache.get('FOO', { getValue: getValue })).to.eql(2);
chai_1.expect(count).to.eql(2);
});
it('does not retreive default value', function () {

@@ -82,2 +133,12 @@ var cache = MemoryCache_1.MemoryCache.create();

});
it('clears subset (filter)', function () {
var cache = MemoryCache_1.MemoryCache.create();
cache
.put('FOO/1', 1)
.put('FOO/2', 2)
.put('BAR/1', 1);
chai_1.expect(cache.keys).to.eql(['FOO/1', 'FOO/2', 'BAR/1']);
cache.clear({ filter: function (key) { return key.startsWith('FOO/'); } });
chai_1.expect(cache.keys).to.eql(['BAR/1']);
});
it('expires values', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {

@@ -84,0 +145,0 @@ var cache;

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

export declare type MemoryCacheFilter = (key: string) => boolean;
export declare type MemoryCacheGetValue<V> = () => V;
export declare type IMemoryCacheGetOptions<V> = {
getValue?: MemoryCacheGetValue<V>;
force?: boolean;
};
export declare type IMemoryCache<K extends string = string> = {

@@ -5,6 +11,9 @@ readonly ttl: number | undefined;

exists(key: K): boolean;
get<V>(key: K, defaultValue?: () => V): V;
get<V>(key: K, args?: MemoryCacheGetValue<V> | IMemoryCacheGetOptions<V>): V;
getAsync<V>(key: K, args?: MemoryCacheGetValue<V> | IMemoryCacheGetOptions<V>): Promise<V>;
put<V>(key: K, value: V): IMemoryCache<K>;
delete(key: K): IMemoryCache<K>;
clear(): IMemoryCache<K>;
clear(args?: {
filter?: MemoryCacheFilter;
}): IMemoryCache<K>;
};
{
"name": "@platform/cache",
"version": "0.1.1",
"version": "0.2.0",
"description": "Caching tools.",

@@ -5,0 +5,0 @@ "main": "lib/index",

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