Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
9eec43044d | |||
4df1274094 | |||
1edda548a1 | |||
c94b5e0009 | |||
fd5f5e357c | |||
57f50ef507 | |||
58bcf0cea2 | |||
bf2d3f661e | |||
eef3fd7c1f | |||
219944218c | |||
00364d261a | |||
04aa6b2fb5 | |||
e113ce5e8e | |||
3e2086fe34 | |||
f9e7204294 | |||
0223581938 | |||
073d792497 |
@@ -26,6 +26,7 @@ mirror:
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
@@ -36,21 +37,11 @@ snyk:
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
@@ -62,6 +53,7 @@ testLTS:
|
||||
testSTABLE:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
@@ -117,8 +109,10 @@ pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g npmpage
|
||||
- npmci command npmpage
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
@@ -129,13 +123,3 @@ pages:
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
allow_failure: true
|
||||
|
@@ -1 +0,0 @@
|
||||
.gitignore: ../gitignore/
|
@@ -1,8 +1,6 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [
|
||||
"@gitzone/npmts",
|
||||
"ts-node"
|
||||
]
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
}
|
||||
}
|
||||
|
1135
package-lock.json
generated
1135
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "smartcache",
|
||||
"version": "1.0.2",
|
||||
"name": "@pushrocks/smartcache",
|
||||
"version": "1.0.11",
|
||||
"private": false,
|
||||
"description": "cache things in smart ways",
|
||||
"main": "dist/index.js",
|
||||
@@ -8,17 +8,23 @@
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tsrun test/test.ts)",
|
||||
"build": "(npmts)"
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^10.5.2"
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.13",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.12.11",
|
||||
"tslint": "^5.11.0",
|
||||
"tslint-config-prettier": "^1.17.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.1",
|
||||
"@pushrocks/smartdelay": "^2.0.2",
|
||||
"@pushrocks/smarterror": "^2.0.1",
|
||||
"@pushrocks/smarthash": "^2.0.0",
|
||||
"@pushrocks/smartpromise": "^2.0.5",
|
||||
"@pushrocks/smarttime": "^2.0.1",
|
||||
"nodehash": "^1.0.4"
|
||||
"@pushrocks/smarttime": "^3.0.5"
|
||||
}
|
||||
}
|
||||
|
75
test/test.ts
75
test/test.ts
@@ -1,36 +1,51 @@
|
||||
// import { expect, tap } from 'tapbundle';
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartcache from '../ts/index';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
|
||||
const smartcacheInstance = new 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);
|
||||
};
|
||||
let smartcacheInstance: smartcache.SmartCache;
|
||||
|
||||
const getResponse2 = 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);
|
||||
};
|
||||
|
||||
getResponse();
|
||||
getResponse();
|
||||
getResponse2();
|
||||
getResponse2();
|
||||
smartdelay.delayFor(1000).then(() => {
|
||||
getResponse();
|
||||
// getResponse2();
|
||||
tap.test('should create a valid instance of SmartCache', async () => {
|
||||
smartcacheInstance = new smartcache.SmartCache();
|
||||
expect(smartcacheInstance).to.be.instanceof(smartcache.SmartCache);
|
||||
});
|
||||
|
||||
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();
|
||||
await smartdelay.delayFor(1000).then(async () => {
|
||||
await getResponse();
|
||||
// getResponse2();
|
||||
});
|
||||
});
|
||||
|
||||
tap.start({
|
||||
throwOnError: true
|
||||
});
|
||||
|
@@ -20,9 +20,12 @@ export class CacheManager {
|
||||
}
|
||||
|
||||
stillValid(identifierArg: string): boolean {
|
||||
if (this.cacheExists(identifierArg) && this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds(
|
||||
this._cacheMap[identifierArg].timer.timeInMilliseconds
|
||||
)) {
|
||||
if (
|
||||
this.cacheExists(identifierArg) &&
|
||||
this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds(
|
||||
this._cacheMap[identifierArg].timer.timeInMilliseconds
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -37,21 +40,23 @@ export class CacheManager {
|
||||
};
|
||||
this._cacheMap[identifierArg].timer.start();
|
||||
this._cacheMap[identifierArg].timer.completed.then(() => {
|
||||
this.deleteCache(identifierArg)
|
||||
this.deleteCache(identifierArg);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* waits for the cache to be ready
|
||||
*/
|
||||
async waitForCacheReady (identifierArg: string) {
|
||||
await this._cacheMap[identifierArg].cachedDeferred.promise
|
||||
async waitForCacheReady(identifierArg: string) {
|
||||
await this._cacheMap[identifierArg].cachedDeferred.promise;
|
||||
return true;
|
||||
}
|
||||
|
||||
setCache(identifierArg: string, cachedObjectArg: any, validForArg = 1000) {
|
||||
if(!this.cacheExists(identifierArg)) {
|
||||
console.log(new SmartError(`Cache for ${identifierArg} has not been announced or timed out!`));
|
||||
if (!this.cacheExists(identifierArg)) {
|
||||
console.log(
|
||||
new SmartError(`Cache for ${identifierArg} has not been announced or timed out!`)
|
||||
);
|
||||
}
|
||||
this._cacheMap[identifierArg].cachedObject = cachedObjectArg;
|
||||
this._cacheMap[identifierArg].cachedDeferred.resolve();
|
||||
|
@@ -6,14 +6,18 @@ export class SmartCache {
|
||||
|
||||
async cacheReturn(asyncCachedFuncArg: () => Promise<any>, cacheDuration: number = 5000) {
|
||||
let callStack: string = new plugins.smarterror.SmartError('').cleanFullStack;
|
||||
let callHash = plugins.nodehash.sha256FromStringSync(callStack);
|
||||
|
||||
console.log(callHash);
|
||||
if(this._cacheManager.cacheExists(callHash) && await this._cacheManager.waitForCacheReady(callHash) && this._cacheManager.stillValid(callHash)) {
|
||||
let callHash = plugins.smarthash.sha256FromStringSync(callStack);
|
||||
|
||||
// console.log(callHash);
|
||||
if (
|
||||
this._cacheManager.cacheExists(callHash) &&
|
||||
(await this._cacheManager.waitForCacheReady(callHash)) &&
|
||||
this._cacheManager.stillValid(callHash)
|
||||
) {
|
||||
return this._cacheManager.getCache(callHash).cachedObject;
|
||||
} else {
|
||||
this._cacheManager.announceCache(callHash, cacheDuration);
|
||||
let newCachedObject = await asyncCachedFuncArg()
|
||||
let newCachedObject = await asyncCachedFuncArg();
|
||||
this._cacheManager.setCache(callHash, newCachedObject, cacheDuration);
|
||||
return newCachedObject;
|
||||
}
|
||||
|
@@ -2,6 +2,6 @@ import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smarterror from '@pushrocks/smarterror';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
import * as nodehash from 'nodehash';
|
||||
import * as smarthash from '@pushrocks/smarthash';
|
||||
|
||||
export { smarterror, smartdelay, smartpromise, smarttime, nodehash };
|
||||
export { smarterror, smartdelay, smartpromise, smarttime, smarthash };
|
||||
|
16
tslint.json
16
tslint.json
@@ -1,3 +1,17 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
|
Reference in New Issue
Block a user