Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
428f159ff7 | |||
abfa1e1c70 | |||
57c2e33a6c | |||
b0fa7931a8 | |||
53d9f8bce1 | |||
6e7205d788 | |||
c57d76376d | |||
b6c4ef568a | |||
c7682e4cc5 | |||
677b07a176 | |||
0e11d0e760 | |||
1f80df6ec2 | |||
05997ef1cf | |||
dc5a93b3a2 | |||
51c1759961 | |||
b5667be823 | |||
f147d48655 |
16
.gitignore
vendored
16
.gitignore
vendored
@ -1,6 +1,20 @@
|
||||
.nogit/
|
||||
node_modules/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
130
.gitlab-ci.yml
130
.gitlab-ci.yml
@ -1,16 +1,19 @@
|
||||
# gitzone standard
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- npm install -g @shipzone/npmci
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
@ -18,102 +21,113 @@ stages:
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testLTS:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
- docker
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
only:
|
||||
- tags
|
||||
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]
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci node install stable
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
@ -121,5 +135,5 @@ pages:
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
- public
|
||||
allow_failure: true
|
||||
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -2,5 +2,31 @@
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartcache",
|
||||
"description": "A module for caching asynchronous function results with smart time and hash-based invalidation strategies.",
|
||||
"npmPackagename": "@push.rocks/smartcache",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"caching",
|
||||
"asynchronous",
|
||||
"time-based invalidation",
|
||||
"hash-based identification",
|
||||
"smart caching",
|
||||
"node.js",
|
||||
"performance optimization",
|
||||
"typescript",
|
||||
"delay handling",
|
||||
"error management"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
}
|
||||
}
|
||||
}
|
1150
package-lock.json
generated
1150
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
68
package.json
68
package.json
@ -1,30 +1,62 @@
|
||||
{
|
||||
"name": "@pushrocks/smartcache",
|
||||
"version": "1.0.11",
|
||||
"name": "@push.rocks/smartcache",
|
||||
"version": "1.0.16",
|
||||
"private": false,
|
||||
"description": "cache things in smart ways",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"description": "A module for caching asynchronous function results with smart time and hash-based invalidation strategies.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
"build": "(tsbuild)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@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"
|
||||
"@gitzone/tsbuild": "^2.1.63",
|
||||
"@gitzone/tsrun": "^1.2.37",
|
||||
"@gitzone/tstest": "^1.0.71",
|
||||
"@pushrocks/tapbundle": "^5.0.3",
|
||||
"@types/node": "^18.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.2",
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smarterror": "^2.0.1",
|
||||
"@pushrocks/smarthash": "^2.0.0",
|
||||
"@pushrocks/smartpromise": "^2.0.5",
|
||||
"@pushrocks/smarttime": "^3.0.5"
|
||||
"@pushrocks/smarthash": "^3.0.1",
|
||||
"@pushrocks/smartpromise": "^3.1.7",
|
||||
"@pushrocks/smarttime": "^4.0.1"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"type": "module",
|
||||
"keywords": [
|
||||
"caching",
|
||||
"asynchronous",
|
||||
"time-based invalidation",
|
||||
"hash-based identification",
|
||||
"smart caching",
|
||||
"node.js",
|
||||
"performance optimization",
|
||||
"typescript",
|
||||
"delay handling",
|
||||
"error management"
|
||||
],
|
||||
"homepage": "https://code.foss.global/push.rocks/smartcache",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://code.foss.global/push.rocks/smartcache.git"
|
||||
}
|
||||
}
|
||||
}
|
4326
pnpm-lock.yaml
generated
Normal file
4326
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
113
readme.md
Normal file
113
readme.md
Normal file
@ -0,0 +1,113 @@
|
||||
# @push.rocks/smartcache
|
||||
Cache things in smart ways.
|
||||
|
||||
## Install
|
||||
To use `@push.rocks/smartcache` in your project, you'll need to install it using npm or yarn. Here's how you can do it:
|
||||
|
||||
```bash
|
||||
npm install @push.rocks/smartcache --save
|
||||
```
|
||||
or if you are using yarn:
|
||||
|
||||
```bash
|
||||
yarn add @push.rocks/smartcache
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
`@push.rocks/smartcache` is designed to cache the results of function calls in a smart and efficient way, significantly improving the performance of repeat function calls by avoiding unnecessary recalculations or remote fetches. This package is especially useful when dealing with data that changes infrequently or expensive computation functions whose results can be reused within a given period. Here's how to use `@push.rocks/smartcache` in your TypeScript projects:
|
||||
|
||||
First, make sure to import `SmartCache`:
|
||||
|
||||
```typescript
|
||||
import { SmartCache } from '@push.rocks/smartcache';
|
||||
```
|
||||
|
||||
### Basic Example
|
||||
|
||||
Create an instance of `SmartCache`:
|
||||
|
||||
```typescript
|
||||
const mySmartCache = new SmartCache();
|
||||
```
|
||||
|
||||
Now, imagine you have an asynchronous function whose result you want to cache:
|
||||
|
||||
```typescript
|
||||
async function fetchData(): Promise<string> {
|
||||
// simulate fetching data
|
||||
await new Promise(resolve => setTimeout(resolve, 1000)); // delay to mimic fetch time
|
||||
return 'Some fetched data';
|
||||
}
|
||||
```
|
||||
|
||||
Using `@push.rocks/smartcache`, you can cache the result of `fetchData` easily:
|
||||
|
||||
```typescript
|
||||
async function getCachedData() {
|
||||
const cachedData = await mySmartCache.cacheReturn(fetchData, 60000); // Cache for 1 minute
|
||||
console.log(cachedData); // 'Some fetched data'
|
||||
}
|
||||
```
|
||||
|
||||
### Advanced Use Cases
|
||||
|
||||
#### Custom Cache Identifiers
|
||||
|
||||
By default, `@push.rocks/smartcache` identifies cache entries based on the call stack. However, when you have dynamic arguments that significantly change the output of the function, you might want to create a custom cache identifier to differentiate between these calls.
|
||||
|
||||
Suppose `fetchData` now takes an argument:
|
||||
|
||||
```typescript
|
||||
async function fetchData(userId: string): Promise<UserData> {
|
||||
// Fetch user data based on userId
|
||||
}
|
||||
```
|
||||
|
||||
You could create a custom cache identifier like so:
|
||||
|
||||
```typescript
|
||||
const userId = '123';
|
||||
const customIdentifier = `fetchData-${userId}`;
|
||||
|
||||
const cachedUserData = await mySmartCache.cacheReturn(() => fetchData(userId), 60000, customIdentifier);
|
||||
```
|
||||
|
||||
#### Clearing the Cache
|
||||
|
||||
In certain situations, you might want to clear the cached data before it expires naturally. `@push.rocks/smartcache` provides a method to manually clear specific cache entries:
|
||||
|
||||
```typescript
|
||||
mySmartCache.clearCache(customIdentifier);
|
||||
```
|
||||
|
||||
### Best Practices
|
||||
|
||||
- **Cache Duration**: Choose a cache duration that makes sense for your data. Highly dynamic data might not benefit much from long cache times, whereas static data can be cached longer.
|
||||
- **Error Handling**: Always implement error handling for your cached functions, especially when dealing with network requests.
|
||||
- **Memory Management**: Be mindful of what you cache. Caching large objects or a high number of entries can lead to increased memory usage.
|
||||
|
||||
### Conclusion
|
||||
|
||||
`@push.rocks/smartcache` is a powerful utility for caching asynchronous function results, significantly improving the performance and efficiency of your applications. By understanding and implementing the basics and exploring advanced use cases, you can leverage `@push.rocks/smartcache` to its full potential.
|
||||
|
||||
Remember, the key to effective caching is understanding your data's behavior and aligning your caching strategy accordingly. Happy caching!
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
37
test/test.ts
37
test/test.ts
@ -1,51 +1,54 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartcache from '../ts/index';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartcache from '../ts/index.js';
|
||||
|
||||
let smartcacheInstance: smartcache.SmartCache;
|
||||
|
||||
tap.test('should create a valid instance of SmartCache', async () => {
|
||||
smartcacheInstance = new smartcache.SmartCache();
|
||||
expect(smartcacheInstance).to.be.instanceof(smartcache.SmartCache);
|
||||
expect(smartcacheInstance).toBeInstanceOf(smartcache.SmartCache);
|
||||
});
|
||||
|
||||
tap.test('try to get async responses', async () => {
|
||||
tap.test('try to get async responses', async (toolsArg) => {
|
||||
let response1Counter = 0;
|
||||
const getResponse = async () => {
|
||||
const response = await smartcacheInstance
|
||||
.cacheReturn(async () => {
|
||||
console.log('function ran');
|
||||
console.log('function 1 ran');
|
||||
response1Counter++;
|
||||
return 'hello';
|
||||
}, 1000)
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log('response is:');
|
||||
console.log(response);
|
||||
};
|
||||
|
||||
let response2Counter = 0;
|
||||
const getResponse2 = async () => {
|
||||
const response = await smartcacheInstance
|
||||
.cacheReturn(async () => {
|
||||
console.log('function 2 ran');
|
||||
return 'hello';
|
||||
response2Counter++;
|
||||
return 'hello there!';
|
||||
}, 1000)
|
||||
.catch(err => {
|
||||
.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();
|
||||
});
|
||||
await getResponse();
|
||||
await getResponse();
|
||||
await getResponse2();
|
||||
await getResponse2();
|
||||
await toolsArg.delayFor(500);
|
||||
await getResponse();
|
||||
await toolsArg.delayFor(2000);
|
||||
await getResponse2();
|
||||
});
|
||||
|
||||
tap.start({
|
||||
throwOnError: true
|
||||
throwOnError: true,
|
||||
});
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartcache',
|
||||
version: '1.0.16',
|
||||
description: 'cache things in smart ways'
|
||||
}
|
@ -1 +1 @@
|
||||
export * from './smartcache.classes.smartcache';
|
||||
export * from './smartcache.classes.smartcache.js';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './smartcache.plugins';
|
||||
import * as plugins from './smartcache.plugins.js';
|
||||
import { SmartError } from '@pushrocks/smarterror';
|
||||
|
||||
export interface ICacheMap {
|
||||
@ -12,7 +12,7 @@ export interface ICacheMap {
|
||||
export class CacheManager {
|
||||
private _cacheMap: ICacheMap = {};
|
||||
|
||||
cacheExists(identifierArg): boolean {
|
||||
cacheExists(identifierArg: string): boolean {
|
||||
if (this._cacheMap[identifierArg]) {
|
||||
return true;
|
||||
}
|
||||
@ -36,7 +36,7 @@ export class CacheManager {
|
||||
this._cacheMap[identifierArg] = {
|
||||
timer: new plugins.smarttime.Timer(validForArg),
|
||||
cachedDeferred: new plugins.smartpromise.Deferred(),
|
||||
cachedObject: null
|
||||
cachedObject: null,
|
||||
};
|
||||
this._cacheMap[identifierArg].timer.start();
|
||||
this._cacheMap[identifierArg].timer.completed.then(() => {
|
||||
|
@ -1,12 +1,14 @@
|
||||
import * as plugins from './smartcache.plugins';
|
||||
import { CacheManager } from './smartcache.classes.cachemanager';
|
||||
import * as plugins from './smartcache.plugins.js';
|
||||
import { CacheManager } from './smartcache.classes.cachemanager.js';
|
||||
|
||||
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.smarthash.sha256FromStringSync(callStack);
|
||||
const callStack: string = new plugins.smarterror.SmartError(
|
||||
'Cache Creation Point'
|
||||
).cleanFullStack.split('\n')[2];
|
||||
const callHash = plugins.smarthash.sha256FromStringSync(callStack);
|
||||
|
||||
// console.log(callHash);
|
||||
if (
|
||||
@ -17,7 +19,7 @@ export class SmartCache {
|
||||
return this._cacheManager.getCache(callHash).cachedObject;
|
||||
} else {
|
||||
this._cacheManager.announceCache(callHash, cacheDuration);
|
||||
let newCachedObject = await asyncCachedFuncArg();
|
||||
const newCachedObject = await asyncCachedFuncArg();
|
||||
this._cacheManager.setCache(callHash, newCachedObject, cacheDuration);
|
||||
return newCachedObject;
|
||||
}
|
||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"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"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user