11 Commits

Author SHA1 Message Date
58bcf0cea2 1.0.8 2018-08-22 00:35:03 +02:00
bf2d3f661e fix(remove obsolete console logs): update 2018-08-22 00:35:03 +02:00
eef3fd7c1f 1.0.7 2018-08-19 21:25:21 +02:00
219944218c fix(CI): remove obsolete build dependencies 2018-08-19 21:25:21 +02:00
00364d261a 1.0.6 2018-08-19 21:19:23 +02:00
04aa6b2fb5 fix(dependencies): update 2018-08-19 21:19:23 +02:00
e113ce5e8e 1.0.5 2018-07-13 22:23:40 +02:00
3e2086fe34 fix(package): name 2018-07-13 22:23:40 +02:00
f9e7204294 1.0.4 2018-07-13 22:20:09 +02:00
0223581938 1.0.3 2018-07-12 23:52:07 +02:00
073d792497 fix(package): project setup 2018-07-12 23:52:06 +02:00
7 changed files with 861 additions and 158 deletions

View File

@@ -1 +0,0 @@
.gitignore: ../gitignore/

View File

@@ -1,8 +1,6 @@
{ {
"npmci": { "npmci": {
"npmGlobalTools": [ "npmGlobalTools": [],
"@gitzone/npmts", "npmAccessLevel": "public"
"ts-node"
]
} }
} }

882
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{ {
"name": "smartcache", "name": "@pushrocks/smartcache",
"version": "1.0.2", "version": "1.0.8",
"private": false, "private": false,
"description": "cache things in smart ways", "description": "cache things in smart ways",
"main": "dist/index.js", "main": "dist/index.js",
@@ -8,17 +8,21 @@
"author": "Lossless GmbH", "author": "Lossless GmbH",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "(tsrun test/test.ts)", "test": "(tstest test/)",
"build": "(npmts)" "build": "(tsbuild)"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^10.5.2" "@gitzone/tsbuild": "^2.0.22",
"@gitzone/tsrun": "^1.1.12",
"@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.5",
"@types/node": "^10.7.1"
}, },
"dependencies": { "dependencies": {
"@pushrocks/smartdelay": "^2.0.1", "@pushrocks/smartdelay": "^2.0.2",
"@pushrocks/smarterror": "^2.0.1", "@pushrocks/smarterror": "^2.0.1",
"@pushrocks/smartpromise": "^2.0.5", "@pushrocks/smartpromise": "^2.0.5",
"@pushrocks/smarttime": "^2.0.1", "@pushrocks/smarttime": "^2.0.2",
"nodehash": "^1.0.4" "nodehash": "^1.0.4"
} }
} }

View File

@@ -1,36 +1,51 @@
// import { expect, tap } from 'tapbundle'; import { expect, tap } from "@pushrocks/tapbundle";
import * as smartcache from '../ts/index'; import * as smartcache from "../ts/index";
import * as smartdelay from '@pushrocks/smartdelay'; import * as smartdelay from "@pushrocks/smartdelay";
const smartcacheInstance = new smartcache.SmartCache(); let smartcacheInstance: smartcache.SmartCache;
const getResponse = async () => {
const response = await smartcacheInstance.cacheReturn(async () => {
console.log('function ran')
return 'hello';
}, 1000).catch(err => {
console.log(err);
});
console.log('response is:')
console.log(response);
};
const getResponse2 = async () => { tap.test("should create a valid instance of SmartCache", async () => {
const response = await smartcacheInstance.cacheReturn(async () => { smartcacheInstance = new smartcache.SmartCache();
console.log('function ran') expect(smartcacheInstance).to.be.instanceof(smartcache.SmartCache);
return 'hello';
}, 1000).catch(err => {
console.log(err);
});
console.log('response is:')
console.log(response);
};
getResponse();
getResponse();
getResponse2();
getResponse2();
smartdelay.delayFor(1000).then(() => {
getResponse();
// getResponse2();
}); });
tap.test("try to get async responses", async () => {
const getResponse = async () => {
const response = await smartcacheInstance
.cacheReturn(async () => {
console.log("function ran");
return "hello";
}, 1000)
.catch(err => {
console.log(err);
});
console.log("response is:");
console.log(response);
};
const getResponse2 = async () => {
const response = await smartcacheInstance
.cacheReturn(async () => {
console.log("function 2 ran");
return "hello";
}, 1000)
.catch(err => {
console.log(err);
});
console.log("response is:");
console.log(response);
};
getResponse();
getResponse();
getResponse2();
getResponse2();
smartdelay.delayFor(1000).then(() => {
getResponse();
// getResponse2();
});
});
tap.start({
throwOnError: true
});

View File

@@ -20,9 +20,12 @@ export class CacheManager {
} }
stillValid(identifierArg: string): boolean { stillValid(identifierArg: string): boolean {
if (this.cacheExists(identifierArg) && this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds( if (
this._cacheMap[identifierArg].timer.timeInMilliseconds this.cacheExists(identifierArg) &&
)) { this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds(
this._cacheMap[identifierArg].timer.timeInMilliseconds
)
) {
return true; return true;
} }
return false; return false;
@@ -37,21 +40,23 @@ export class CacheManager {
}; };
this._cacheMap[identifierArg].timer.start(); this._cacheMap[identifierArg].timer.start();
this._cacheMap[identifierArg].timer.completed.then(() => { this._cacheMap[identifierArg].timer.completed.then(() => {
this.deleteCache(identifierArg) this.deleteCache(identifierArg);
}); });
} }
/** /**
* waits for the cache to be ready * waits for the cache to be ready
*/ */
async waitForCacheReady (identifierArg: string) { async waitForCacheReady(identifierArg: string) {
await this._cacheMap[identifierArg].cachedDeferred.promise await this._cacheMap[identifierArg].cachedDeferred.promise;
return true; return true;
} }
setCache(identifierArg: string, cachedObjectArg: any, validForArg = 1000) { setCache(identifierArg: string, cachedObjectArg: any, validForArg = 1000) {
if(!this.cacheExists(identifierArg)) { if (!this.cacheExists(identifierArg)) {
console.log(new SmartError(`Cache for ${identifierArg} has not been announced or timed out!`)); console.log(
new SmartError(`Cache for ${identifierArg} has not been announced or timed out!`)
);
} }
this._cacheMap[identifierArg].cachedObject = cachedObjectArg; this._cacheMap[identifierArg].cachedObject = cachedObjectArg;
this._cacheMap[identifierArg].cachedDeferred.resolve(); this._cacheMap[identifierArg].cachedDeferred.resolve();

View File

@@ -7,13 +7,17 @@ export class SmartCache {
async cacheReturn(asyncCachedFuncArg: () => Promise<any>, cacheDuration: number = 5000) { async cacheReturn(asyncCachedFuncArg: () => Promise<any>, cacheDuration: number = 5000) {
let callStack: string = new plugins.smarterror.SmartError('').cleanFullStack; let callStack: string = new plugins.smarterror.SmartError('').cleanFullStack;
let callHash = plugins.nodehash.sha256FromStringSync(callStack); let callHash = plugins.nodehash.sha256FromStringSync(callStack);
console.log(callHash); // console.log(callHash);
if(this._cacheManager.cacheExists(callHash) && await this._cacheManager.waitForCacheReady(callHash) && this._cacheManager.stillValid(callHash)) { if (
this._cacheManager.cacheExists(callHash) &&
(await this._cacheManager.waitForCacheReady(callHash)) &&
this._cacheManager.stillValid(callHash)
) {
return this._cacheManager.getCache(callHash).cachedObject; return this._cacheManager.getCache(callHash).cachedObject;
} else { } else {
this._cacheManager.announceCache(callHash, cacheDuration); this._cacheManager.announceCache(callHash, cacheDuration);
let newCachedObject = await asyncCachedFuncArg() let newCachedObject = await asyncCachedFuncArg();
this._cacheManager.setCache(callHash, newCachedObject, cacheDuration); this._cacheManager.setCache(callHash, newCachedObject, cacheDuration);
return newCachedObject; return newCachedObject;
} }