Skip to content

Commit befbb34

Browse files
committed
✨ 💯 cache time
1 parent 082d7b4 commit befbb34

File tree

4 files changed

+25
-41
lines changed

4 files changed

+25
-41
lines changed

Client/app/api.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,23 @@ export function hashCode(str) {
2323

2424
@Injectable()
2525
export class ApiService {
26-
constructor(public _http: Http) {
2726

28-
}
27+
constructor(public _http: Http, public _cache: CacheService) {
2928

30-
/**
31-
* whatever domain/feature method name
32-
*/
33-
get(url: string, options?: any) {
34-
return this._http.get(url, options)
35-
.map(res => res.json())
36-
.catch(err => {
37-
console.log('Error: ', err);
38-
return Observable.throw(err);
39-
});
4029
}
41-
42-
}
43-
44-
@Injectable()
45-
export class ModelService {
46-
47-
constructor(public _api: ApiService, public _cache: CacheService) {
48-
49-
}
50-
51-
/**
52-
* whatever domain/feature method name
53-
*/
54-
get(url) {
55-
// you want to return the cache if there is a response in it. This would cache the first response so if your API isn't idempotent you probably want to remove the item from the cache after you use it. LRU of 1
30+
// whatever domain/feature method name
31+
getModel(url) {
32+
// you want to return the cache if there is a response in it. This would cache the first response so if your API isn't idempotent you probably want to remove the item from the cache after you use it. LRU of 1
5633
let key = url;
57-
5834
if (this._cache.has(key)) {
5935
return Observable.of(this._cache.get(key));
6036
}
6137
// you probably shouldn't .share() and you should write the correct logic
62-
return this._api.get(url)
38+
return this._http.get(url)
39+
.map(res => res.json())
6340
.do(json => {
6441
this._cache.set(key, json);
6542
})
6643
.share();
6744
}
68-
6945
}

Client/app/app.browser.module.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { UniversalModule, isBrowser, isNode } from 'angular2-universal/browser';
77

88
// Universal : XHR Cache
99
import { CacheService } from './universal-cache';
10-
import { ApiService, ModelService } from './api';
10+
import { ApiService } from './api';
1111
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
1212

1313
// Bootstrap (non-jQuery implementation)
@@ -45,8 +45,7 @@ import { SocketConnectionService, WebSocketService } from 'app-shared';
4545
{ provide: 'isBrowser', useValue: isBrowser },
4646
{ provide: 'isNode', useValue: isNode },
4747
CacheService,
48-
ApiService,
49-
ModelService
48+
ApiService
5049
],
5150
imports: [
5251
UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
@@ -68,12 +67,15 @@ export class AppModule {
6867
}
6968

7069
doRehydrate() {
70+
console.log('DO REHYDRATE');
7171
let defaultValue = {};
7272
let serverCache = this._getCacheValue(CacheService.KEY, defaultValue);
7373
this.cache.rehydrate(serverCache);
7474
}
7575

7676
_getCacheValue(key: string, defaultValue: any): any {
77+
console.log('_getCacheValue for ' + key)
78+
console.log(window[UNIVERSAL_KEY]);
7779
// browser
7880
const win: any = window;
7981
if (win[UNIVERSAL_KEY] && win[UNIVERSAL_KEY][key]) {

Client/app/app.server.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { UniversalModule, isBrowser, isNode } from 'angular2-universal/node';
77

88
// Universal : XHR Cache
99
import { CacheService } from './universal-cache';
10-
import { ApiService, ModelService } from './api';
10+
import { ApiService } from './api';
1111

1212
// Bootstrap (non-jQuery implementation)
1313
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
@@ -44,8 +44,7 @@ import { SocketConnectionService, WebSocketService } from 'app-shared';
4444
{ provide: 'isBrowser', useValue: isBrowser },
4545
{ provide: 'isNode', useValue: isNode },
4646
CacheService,
47-
ApiService,
48-
ModelService
47+
ApiService
4948
],
5049
imports: [
5150
UniversalModule, // Must be first import. This automatically imports NodeModule, NodeHttpModule, and NodeJsonpModule too.
@@ -70,6 +69,7 @@ export class AppModule {
7069
* in Universal for now until it's fixed
7170
*/
7271
universalDoDehydrate = (universalCache) => {
72+
console.log('universalDoDehydrate ****');
7373
universalCache[CacheService.KEY] = JSON.stringify(this.cache.dehydrate());
7474
}
7575

Client/containers/rest-test/rest-test.component.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { Http } from '@angular/http';
33

4+
import { ApiService } from '../../app/api';
5+
46
@Component({
57
selector: 'rest-test',
68
template: require('./rest-test.component.html')
79
})
8-
export class RestTestComponent {
10+
export class RestTestComponent implements OnInit {
911
public users: IUser[];
1012

11-
constructor(private http: Http) {
12-
this.http.get('/api/test/users').subscribe(result => {
13-
this.users = result.json();
13+
constructor(private _apiService: ApiService) { }
14+
15+
ngOnInit() {
16+
this._apiService.getModel('/api/test/users').subscribe(result => {
17+
console.log('AFTER SUBSCRIBE');
18+
console.log(result);
19+
this.users = result;
1420
});
1521
}
1622
}

0 commit comments

Comments
 (0)