Skip to content

Commit 2a2a600

Browse files
committed
fix #1
increased performance
1 parent 3dfc13c commit 2a2a600

File tree

9 files changed

+114
-15
lines changed

9 files changed

+114
-15
lines changed

e2e/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
<script src="dist/index.js"></script>
7+
</head>
8+
<body>
9+
10+
</body>
11+
</html>

e2e/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "test-ts-method-cache",
3+
"version": "0.1.2",
4+
"dependencies": {
5+
"rxjs": "^5.4.3"
6+
},
7+
"devDependencies": {
8+
"@types/node": "^7.0.39",
9+
"typescript": "^2.4.2"
10+
},
11+
"scripts": {
12+
"build": "tsc -p tsconfig.json"
13+
}
14+
}

e2e/src/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {Observable} from 'rxjs';
2+
import {MemoryCache} from "../../src/";
3+
import {CacheContainer} from "../../src/core/decorator/cache-container.decorator";
4+
import {MethodCacheService} from "../../src/core/service/method-cache.service";
5+
6+
@CacheContainer('hii')
7+
class TestThis {
8+
9+
@MemoryCache()
10+
public methodTest(paramA: string, paramB: string, paramC: number): Observable<number> {
11+
console.log(paramA);
12+
console.log(paramB);
13+
console.log(paramC);
14+
return Observable.create(observer=>observer.next(paramA + paramC + paramB));
15+
}
16+
17+
}
18+
19+
let test = new TestThis();
20+
let obs = test.methodTest('stringA', 'stringB', 2);
21+
let obs3 = test.methodTest('stringA', 'stringB', 2);
22+
let cache = new MethodCacheService();
23+
cache.clearMemoryContainer('hii');
24+
let obs2 = test.methodTest('stringA', 'stringB', 2);
25+
obs.subscribe((res) => {
26+
console.log(res);
27+
});
28+
obs3.subscribe((res) => {
29+
console.log(res);
30+
});
31+
obs2.subscribe((res) => {
32+
console.log(res);
33+
});

e2e/tsconfig.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "",
4+
"target": "es5",
5+
"module": "commonjs",
6+
"moduleResolution": "node",
7+
"declaration": false,
8+
"noImplicitAny": false,
9+
"strictNullChecks": true,
10+
"sourceMap": false,
11+
"experimentalDecorators": true,
12+
"outDir": "./dist",
13+
"skipLibCheck": true,
14+
"stripInternal": true,
15+
"typeRoots": ["./node_modules/@types"],
16+
"types": [
17+
"node"
18+
],
19+
"lib": [
20+
"es2017", "dom"
21+
]
22+
},
23+
"compileOnSave": false,
24+
"buildOnSave": false,
25+
"files": [
26+
"src/index.ts"
27+
],
28+
"exclude": [
29+
".idea",
30+
"e2e",
31+
"dist",
32+
"coverage",
33+
"node_modules"
34+
]
35+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-method-cache",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"license": "Apache License",
55
"author": {
66
"name": "Poul Kruijt",
@@ -29,7 +29,9 @@
2929
"memory",
3030
"typescript",
3131
"http",
32-
"method"
32+
"method",
33+
"storage",
34+
"session"
3335
],
3436
"peerDependencies": {
3537
"typescript": "^2.2.0"

src/core/decorator/cache-container.decorator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Cache container decorator is properly set', () => {
1111
}
1212

1313
it("should have a cache container options where the key equals the passed key", () => {
14-
let options: CacheContainerOptions = getCacheContainer(TestCache);
14+
let options: CacheContainerOptions = getCacheContainer(TestCache) as CacheContainerOptions;
1515
expect(options.key).toEqual(key);
1616
});
1717

src/core/resolver/method-cache-provider.resolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ export function setCacheContainer<T extends Function>(container: T, options: Cac
3535
}
3636
}
3737

38-
export function getCacheContainer<T extends Function>(container: T): CacheContainerOptions {
39-
return <CacheContainerOptions>containers.get(container);
38+
export function getCacheContainer<T extends Function>(container: T): CacheContainerOptions|undefined {
39+
return containers.get(container);
4040
}

src/core/util/decorator.util.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,23 @@ export function createCacheDecorator<T extends Function>(type: CacheType, target
1010

1111
const provider: BaseCacheProvider = getMethodCacheProvider(type);
1212

13-
let container: CacheContainerOptions;
13+
let cacheObject: BaseCacheObject = provider.getCacheObject(options.key!) || provider.createCacheObject(options);
14+
15+
let container: CacheContainerOptions|undefined|null = null;
1416

1517
return <any>function (...args: any[]): any {
1618

1719
const argsString: string = JSON.stringify(args) || 'void';
1820

19-
let cacheObject: BaseCacheObject = provider.getCacheObject(options.key!);
20-
21-
if (!cacheObject) {
22-
container = container || getCacheContainer(target.constructor);
23-
cacheObject = provider.createCacheObject(options);
21+
if (container === null) {
22+
container = getCacheContainer(target.constructor);
2423
if (container) {
2524
provider.addToContainer(container, cacheObject);
2625
}
27-
} else if (cacheObject.isExpired(argsString)) {
28-
provider.clearCacheArgs(cacheObject, argsString);
2926
}
3027

31-
if (!cacheObject.hasCache(argsString)) {
32-
provider.setCache(options, argsString, method.call(this, args));
28+
if (!cacheObject.hasCache(argsString) || cacheObject.isExpired(argsString)) {
29+
provider.setCache(options, argsString, method.call(this, ...args));
3330
}
3431

3532
return cacheObject.getCache(argsString);

tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,12 @@
2727
"buildOnSave": false,
2828
"includeDefinition": [
2929
"./src/**/*"
30+
],
31+
"exclude": [
32+
".idea",
33+
"e2e",
34+
"dist",
35+
"coverage",
36+
"node_modules"
3037
]
3138
}

0 commit comments

Comments
 (0)