This commit is contained in:
Philipp Kunz 2018-07-12 21:32:20 +02:00
commit 44ca069df8
12 changed files with 596 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.nogit/
node_modules/
coverage/
public/
pages/
.yarn/

141
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,141 @@
# gitzone standard
image: hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages:
- security
- test
- release
- metadata
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
tags:
- docker
- notpriv
snyk:
stage: security
script:
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
# ====================
# 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 node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
stage: test
script:
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
only:
- tags
tags:
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SOURCE_CODE="$PWD"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata
script:
- npmci command npm install -g npmpage
- npmci command npmpage
tags:
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
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
dependencies.yml Normal file
View File

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

8
npmextra.json Normal file
View File

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

281
package-lock.json generated Normal file
View File

@ -0,0 +1,281 @@
{
"name": "smartcache",
"version": "1.0.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@pushrocks/smartdelay": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@pushrocks/smartdelay/-/smartdelay-2.0.1.tgz",
"integrity": "sha512-olWwh2/JWfhmYdgqrR5RdSsgv1GlXBnbP+XKUrdKVk3dcCzkoqDx9lRE9NfpoCg1cUM2VMxbTVgTW9PWyexEuw==",
"requires": {
"@pushrocks/smartpromise": "^2.0.5"
}
},
"@pushrocks/smarterror": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@pushrocks/smarterror/-/smarterror-2.0.1.tgz",
"integrity": "sha512-3OrF5me+/sy5VgwR/tfCqs7qhb0Ywzgn8tTThRUZnCGas0aindISzMiW7cIro3RlFykmtPmdTztC9Ostu2ioeA==",
"requires": {
"clean-stack": "^1.3.0",
"make-error-cause": "^2.2.0"
}
},
"@pushrocks/smartpromise": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/@pushrocks/smartpromise/-/smartpromise-2.0.5.tgz",
"integrity": "sha512-9j/chLtIiNkR0MDw7Mpxg9slxAVvAQwUZuiaPYX5KpHdKxQaHLI1VZ8IN0vPhwlfgNO4i4vGXV0wB8BvSDj03g=="
},
"@pushrocks/smarttime": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@pushrocks/smarttime/-/smarttime-2.0.1.tgz",
"integrity": "sha512-wXt0gMAQv1ix3I1ekfe4xoW440SVmYazCazDioyNZXdY0i3Hvecmr2q73rgHqcwP1he0KJBsXVpivrCOuiAQPA==",
"requires": {
"moment": "^2.21.0",
"smartdelay": "^1.0.4",
"smartq": "^1.1.6"
}
},
"@types/node": {
"version": "10.5.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.2.tgz",
"integrity": "sha512-m9zXmifkZsMHZBOyxZWilMwmTlpC8x5Ty360JKTiXvlXZfBWYpsg9ZZvP/Ye+iZUh+Q+MxDLjItVTWIsfwz+8Q=="
},
"@types/q": {
"version": "0.0.37",
"resolved": "https://registry.npmjs.org/@types/q/-/q-0.0.37.tgz",
"integrity": "sha512-vjFGX1zMTMz/kUp3xgfJcxMVLkMWVMrdlyc0RwVyve1y9jxwqNaT8wTcv6M51ylq2a/zn5lm8g7qPSoIS4uvZQ=="
},
"@types/through2": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/@types/through2/-/through2-0.4.1.tgz",
"integrity": "sha1-DwCOF0wdxfHMfgDrLRMrLLZ/e9I=",
"requires": {
"@types/node": "*"
}
},
"clean-stack": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-1.3.0.tgz",
"integrity": "sha1-noIVAa6XmYbEax1m0tQy2y/UrjE="
},
"core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"define-properties": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.2.tgz",
"integrity": "sha1-g6c/L+pWmJj7c3GTyPhzyvbUXJQ=",
"requires": {
"foreach": "^2.0.5",
"object-keys": "^1.0.8"
}
},
"es-abstract": {
"version": "1.12.0",
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.12.0.tgz",
"integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==",
"requires": {
"es-to-primitive": "^1.1.1",
"function-bind": "^1.1.1",
"has": "^1.0.1",
"is-callable": "^1.1.3",
"is-regex": "^1.0.4"
}
},
"es-to-primitive": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.1.1.tgz",
"integrity": "sha1-RTVSSKiJeQNLZ5Lhm7gfK3l13Q0=",
"requires": {
"is-callable": "^1.1.1",
"is-date-object": "^1.0.1",
"is-symbol": "^1.0.1"
}
},
"foreach": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/foreach/-/foreach-2.0.5.tgz",
"integrity": "sha1-C+4AUBiusmDQo6865ljdATbsG5k="
},
"function-bind": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
},
"has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
"integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
"requires": {
"function-bind": "^1.1.1"
}
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
},
"is-callable": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz",
"integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA=="
},
"is-date-object": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz",
"integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY="
},
"is-regex": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz",
"integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=",
"requires": {
"has": "^1.0.1"
}
},
"is-symbol": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.1.tgz",
"integrity": "sha1-PMWfAAJRlLarLjjbrmaJJWtmBXI="
},
"isarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
},
"make-error": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.4.tgz",
"integrity": "sha512-0Dab5btKVPhibSalc9QGXb559ED7G7iLjFXBaj9Wq8O3vorueR5K5jaE3hkG6ZQINyhA/JgG6Qk4qdFQjsYV6g=="
},
"make-error-cause": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/make-error-cause/-/make-error-cause-2.2.0.tgz",
"integrity": "sha512-qPkj0ajfb7j8dSevKEmVE+Pc5x7OlFy9aQN6ryYNSCn+6METm45Pz5TfuB6uiYyYTyI1UYmnsfMjytQwb6Q5zw==",
"requires": {
"make-error": "^1.3.4"
}
},
"moment": {
"version": "2.22.2",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.22.2.tgz",
"integrity": "sha1-PCV/mDn8DpP/UxSWMiOeuQeD/2Y="
},
"nodehash": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/nodehash/-/nodehash-1.0.4.tgz",
"integrity": "sha1-/XMzl6CSW5tLwZcm88DmiFAgbn8=",
"requires": {
"@types/q": "0.x.x",
"@types/through2": "0.x.x",
"q": "^1.4.1",
"through2": "^2.0.1",
"typings-global": "^1.0.6"
}
},
"object-keys": {
"version": "1.0.12",
"resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz",
"integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag=="
},
"object.getownpropertydescriptors": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz",
"integrity": "sha1-h1jIRvW0B62rDyNuCYbxSwUcqhY=",
"requires": {
"define-properties": "^1.1.2",
"es-abstract": "^1.5.1"
}
},
"process-nextick-args": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz",
"integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw=="
},
"q": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz",
"integrity": "sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc="
},
"readable-stream": {
"version": "2.3.6",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
"integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"smartdelay": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/smartdelay/-/smartdelay-1.0.4.tgz",
"integrity": "sha512-ab+d8ADiMMPKwrQNhoJYjmOR20VIIIxjknoEF9/PfMryic7tmvTmFrDMdcnMRohGNw/pN+4/I5dSUBdsDya7uw==",
"requires": {
"smartq": "^1.1.1",
"typings-global": "^1.0.16"
}
},
"smartq": {
"version": "1.1.8",
"resolved": "https://registry.npmjs.org/smartq/-/smartq-1.1.8.tgz",
"integrity": "sha512-FURlYW/C3bLeZjJcBVyw7bxCyQoCXiXbLCZcDUKznhXHaLu35c8m33/a2H8CA0rtb82lvzN8dRLnBZAcNMzNHQ==",
"requires": {
"util.promisify": "^1.0.0"
}
},
"string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"requires": {
"safe-buffer": "~5.1.0"
}
},
"through2": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/through2/-/through2-2.0.3.tgz",
"integrity": "sha1-AARWmzfHx0ujnEPzzteNGtlBQL4=",
"requires": {
"readable-stream": "^2.1.5",
"xtend": "~4.0.1"
}
},
"typings-global": {
"version": "1.0.28",
"resolved": "https://registry.npmjs.org/typings-global/-/typings-global-1.0.28.tgz",
"integrity": "sha512-6VOwJWEY2971HOMHu/7sURzUXiD4/LiMJPsMAOqkHHAtS3MVpLFE5gzTiHilsH9KY5VE1mBQirWIgWFsDuo90A=="
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
},
"util.promisify": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/util.promisify/-/util.promisify-1.0.0.tgz",
"integrity": "sha512-i+6qA2MPhvoKLuxnJNpXAGhg7HphQOSUq2LKMZD0m15EiskXUkMvKdF4Uui0WYeCUGea+o2cw/ZuwehtfsrNkA==",
"requires": {
"define-properties": "^1.1.2",
"object.getownpropertydescriptors": "^2.0.3"
}
},
"xtend": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
"integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68="
}
}
}

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "smartcache",
"version": "1.0.1",
"private": false,
"description": "cache things in smart ways",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tsrun test/test.ts)",
"build": "(npmts)"
},
"devDependencies": {
"@types/node": "^10.5.2"
},
"dependencies": {
"@pushrocks/smartdelay": "^2.0.1",
"@pushrocks/smarterror": "^2.0.1",
"@pushrocks/smartpromise": "^2.0.5",
"@pushrocks/smarttime": "^2.0.1",
"nodehash": "^1.0.4"
}
}

36
test/test.ts Normal file
View File

@ -0,0 +1,36 @@
// import { expect, tap } from '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);
};
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();
});

1
ts/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './smartcache.classes.smartcache';

View File

@ -0,0 +1,67 @@
import * as plugins from './smartcache.plugins';
import { SmartError } from '@pushrocks/smarterror';
export interface ICacheMap {
[key: string]: {
timer: plugins.smarttime.Timer;
cachedDeferred: plugins.smartpromise.Deferred<any>;
cachedObject: any;
};
}
export class CacheManager {
private _cacheMap: ICacheMap = {};
cacheExists(identifierArg): boolean {
if (this._cacheMap[identifierArg]) {
return true;
}
return false;
}
stillValid(identifierArg: string): boolean {
if (this.cacheExists(identifierArg) && this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds(
this._cacheMap[identifierArg].timer.timeInMilliseconds
)) {
return true;
}
return false;
}
// announce the caching of something
announceCache(identifierArg: string, validForArg: number) {
this._cacheMap[identifierArg] = {
timer: new plugins.smarttime.Timer(validForArg),
cachedDeferred: new plugins.smartpromise.Deferred(),
cachedObject: null
};
this._cacheMap[identifierArg].timer.start();
this._cacheMap[identifierArg].timer.completed.then(() => {
this.deleteCache(identifierArg)
});
}
/**
* waits for the cache to be ready
*/
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!`));
}
this._cacheMap[identifierArg].cachedObject = cachedObjectArg;
this._cacheMap[identifierArg].cachedDeferred.resolve();
}
getCache(identifierArg: string) {
return this._cacheMap[identifierArg];
}
async deleteCache(identifierArg: string) {
delete this._cacheMap[identifierArg];
}
}

View File

@ -0,0 +1,21 @@
import * as plugins from './smartcache.plugins';
import { CacheManager } from './smartcache.classes.cachemanager';
export class SmartCache {
private _cacheManager = new CacheManager();
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)) {
return this._cacheManager.getCache(callHash).cachedObject;
} else {
this._cacheManager.announceCache(callHash, cacheDuration);
let newCachedObject = await asyncCachedFuncArg()
this._cacheManager.setCache(callHash, newCachedObject, cacheDuration);
return newCachedObject;
}
}
}

7
ts/smartcache.plugins.ts Normal file
View File

@ -0,0 +1,7 @@
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';
export { smarterror, smartdelay, smartpromise, smarttime, nodehash };

3
tslint.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "tslint-config-standard"
}