1
- import { Component , OnInit } from '@angular/core' ;
1
+ import { Component , OnInit , inject , model , signal } from '@angular/core' ;
2
+ import { HttpClient , HttpHeaders } from '@angular/common/http' ;
3
+ import { lastValueFrom } from 'rxjs' ;
4
+ import { CommonModule } from '@angular/common' ;
5
+ import { FormsModule } from '@angular/forms' ;
2
6
import {
3
7
NgHttpCachingService ,
4
8
NgHttpCachingHeaders ,
5
9
NgHttpCachingConfig ,
6
10
NgHttpCachingHeadersList ,
7
11
withNgHttpCachingContext
8
12
} from '../../../ng-http-caching/src/public-api' ;
9
- import { HttpClient , HttpHeaders } from '@angular/common/http' ;
10
- import { lastValueFrom } from 'rxjs' ;
11
- import { CommonModule } from '@angular/common' ;
12
- import { FormsModule } from '@angular/forms' ;
13
13
14
14
interface CachedKey {
15
15
key : string ;
@@ -18,31 +18,32 @@ interface CachedKey {
18
18
}
19
19
20
20
@Component ( {
21
- // eslint-disable-next-line @angular-eslint/component-selector
22
- selector : 'app-root' ,
23
- templateUrl : './app.component.html' ,
24
- styleUrls : [ './app.component.css' ] ,
25
- imports : [ CommonModule , FormsModule ]
21
+ // eslint-disable-next-line @angular-eslint/component-selector
22
+ selector : 'app-root' ,
23
+ templateUrl : './app.component.html' ,
24
+ styleUrls : [ './app.component.css' ] ,
25
+ imports : [ CommonModule , FormsModule ]
26
26
} )
27
27
export class AppComponent implements OnInit {
28
- public url = 'https://my-json-server.typicode.com/typicode/demo/db' ;
29
- public key = 'GET@' + this . url ;
30
- public tag = '' ;
31
- public regex : string = '' ;
32
- public cachedKeys : CachedKey [ ] = [ ] ;
33
- public timeSpan : number | null = null ;
34
- public nocache = false ;
35
- public lifetime = null ;
36
- public count = 0 ;
37
- public typeOfRequests = [ 'PARALLEL' , 'SEQUENTIAL' , 'NESTED' ] ;
38
- public typeOfRequest = this . typeOfRequests [ 0 ] ;
39
- private config : NgHttpCachingConfig ;
28
+ private readonly ngHttpCachingService = inject ( NgHttpCachingService ) ;
29
+ private readonly http = inject ( HttpClient ) ;
30
+
31
+ public readonly url = model ( 'https://my-json-server.typicode.com/typicode/demo/db' ) ;
32
+ public readonly key = model ( 'GET@' + this . url ( ) ) ;
33
+ public readonly tag = model ( '' ) ;
34
+ public readonly regex = model ( '' ) ;
35
+ public readonly cachedKeys = signal < CachedKey [ ] > ( [ ] ) ;
36
+ public readonly timeSpan = signal < number | null > ( null ) ;
37
+ public readonly nocache = signal ( false ) ;
38
+ public readonly lifetime = model < number | null > ( null ) ;
39
+ public readonly count = signal ( 0 ) ;
40
+ public readonly typeOfRequests = signal ( [ 'PARALLEL' , 'SEQUENTIAL' , 'NESTED' ] ) ;
41
+ public readonly typeOfRequest = model ( this . typeOfRequests ( ) [ 0 ] ) ;
42
+
43
+ private readonly config : NgHttpCachingConfig ;
40
44
private timer ! : ReturnType < typeof setTimeout > ;
41
45
42
- constructor (
43
- private ngHttpCachingService : NgHttpCachingService ,
44
- private http : HttpClient
45
- ) {
46
+ constructor ( ) {
46
47
this . config = this . ngHttpCachingService . getConfig ( ) ;
47
48
}
48
49
@@ -51,21 +52,29 @@ export class AppComponent implements OnInit {
51
52
}
52
53
53
54
async getRequest ( ) : Promise < void > {
54
- this . timeSpan = null ;
55
- this . count = 0 ;
55
+ this . timeSpan . set ( null ) ;
56
+ this . count . set ( 0 ) ;
56
57
const timeStart = new Date ( ) ;
57
58
59
+ /**
60
+ * @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#headers
61
+ */
58
62
let headers = new HttpHeaders ( ) ;
59
63
if ( this . tag ) {
60
- headers = headers . set ( NgHttpCachingHeaders . TAG , this . tag ) ;
64
+ headers = headers . set ( NgHttpCachingHeaders . TAG , this . tag ( ) ) ;
61
65
}
62
- if ( this . nocache ) {
66
+ if ( this . nocache ( ) ) {
63
67
headers = headers . set ( NgHttpCachingHeaders . DISALLOW_CACHE , '1' ) ;
64
68
}
65
- if ( this . lifetime && Number ( this . lifetime ) !== this . config . lifetime ) {
66
- headers = headers . set ( NgHttpCachingHeaders . LIFETIME , this . lifetime ) ;
69
+ const lifetime = this . lifetime ( ) ;
70
+ if ( lifetime && Number ( lifetime ) !== this . config . lifetime ) {
71
+ headers = headers . set ( NgHttpCachingHeaders . LIFETIME , lifetime . toString ( ) ) ;
67
72
}
68
73
74
+ /**
75
+ * You can override NgHttpCachingConfig
76
+ * @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#httpcontext
77
+ */
69
78
const context = withNgHttpCachingContext ( {
70
79
isExpired : ( ) => {
71
80
console . log ( 'context:isExpired' ) ;
@@ -81,45 +90,45 @@ export class AppComponent implements OnInit {
81
90
}
82
91
} ) ;
83
92
84
- switch ( this . typeOfRequest ) {
93
+ switch ( this . typeOfRequest ( ) ) {
85
94
case 'SEQUENTIAL' : {
86
95
// test sequential requests
87
96
const result1 = await lastValueFrom (
88
- this . http . get ( this . url , { headers, context } )
97
+ this . http . get ( this . url ( ) , { headers, context } )
89
98
) ;
90
99
console . log ( 'Sequential response 1' , result1 ) ;
91
- this . count ++ ;
100
+ this . count . update ( value => value + 1 ) ;
92
101
const result2 = await lastValueFrom (
93
- this . http . get ( this . url , { headers, context } )
102
+ this . http . get ( this . url ( ) , { headers, context } )
94
103
) ;
95
104
console . log ( 'Sequential response 2' , result2 ) ;
96
- this . count ++ ;
97
- this . timeSpan = new Date ( ) . getTime ( ) - timeStart . getTime ( ) ;
105
+ this . count . update ( value => value + 1 ) ;
106
+ this . timeSpan . set ( new Date ( ) . getTime ( ) - timeStart . getTime ( ) ) ;
98
107
this . updateCachedKeys ( ) ;
99
108
break ;
100
109
}
101
110
case 'PARALLEL' : {
102
111
// test parallel requests
103
112
const results = await Promise . all ( [
104
- lastValueFrom ( this . http . get ( this . url , { headers, context } ) ) ,
105
- lastValueFrom ( this . http . get ( this . url , { headers, context } ) ) ,
113
+ lastValueFrom ( this . http . get ( this . url ( ) , { headers, context } ) ) ,
114
+ lastValueFrom ( this . http . get ( this . url ( ) , { headers, context } ) ) ,
106
115
] ) ;
107
- this . count ++ ;
108
- this . count ++ ;
109
- this . timeSpan = new Date ( ) . getTime ( ) - timeStart . getTime ( ) ;
116
+ this . count . update ( value => value + 1 ) ;
117
+ this . count . update ( value => value + 1 ) ;
118
+ this . timeSpan . set ( new Date ( ) . getTime ( ) - timeStart . getTime ( ) ) ;
110
119
this . updateCachedKeys ( ) ;
111
120
console . log ( 'Parallel responses' , results ) ;
112
121
break ;
113
122
}
114
123
case 'NESTED' : {
115
124
// test nested requests
116
- this . http . get ( this . url , { headers, context } ) . subscribe ( ( result1 ) => {
125
+ this . http . get ( this . url ( ) , { headers, context } ) . subscribe ( ( result1 ) => {
117
126
console . log ( 'Nested response 1' , result1 ) ;
118
- this . count ++ ;
119
- this . http . get ( this . url , { headers, context } ) . subscribe ( ( result2 ) => {
127
+ this . count . update ( value => value + 1 ) ;
128
+ this . http . get ( this . url ( ) , { headers, context } ) . subscribe ( ( result2 ) => {
120
129
console . log ( 'Nested response 2' , result2 ) ;
121
- this . count ++ ;
122
- this . timeSpan = new Date ( ) . getTime ( ) - timeStart . getTime ( ) ;
130
+ this . count . update ( value => value + 1 ) ;
131
+ this . timeSpan . set ( new Date ( ) . getTime ( ) - timeStart . getTime ( ) ) ;
123
132
this . updateCachedKeys ( ) ;
124
133
} ) ;
125
134
} ) ;
@@ -129,28 +138,32 @@ export class AppComponent implements OnInit {
129
138
}
130
139
131
140
clearCache ( ) : void {
141
+ /** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
132
142
this . ngHttpCachingService . clearCache ( ) ;
133
143
this . updateCachedKeys ( ) ;
134
144
}
135
145
136
146
clearCacheByTag ( ) : void {
137
- this . ngHttpCachingService . clearCacheByTag ( this . tag ) ;
147
+ /** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
148
+ this . ngHttpCachingService . clearCacheByTag ( this . tag ( ) ) ;
138
149
this . updateCachedKeys ( ) ;
139
150
}
140
151
141
152
clearCacheByRegex ( ) : void {
142
- this . ngHttpCachingService . clearCacheByRegex ( new RegExp ( this . regex ) ) ;
153
+ /** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
154
+ this . ngHttpCachingService . clearCacheByRegex ( new RegExp ( this . regex ( ) ) ) ;
143
155
this . updateCachedKeys ( ) ;
144
156
}
145
157
146
158
clearCacheByKey ( ) : void {
147
- this . ngHttpCachingService . clearCacheByKey ( this . key ) ;
159
+ /** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
160
+ this . ngHttpCachingService . clearCacheByKey ( this . key ( ) ) ;
148
161
this . updateCachedKeys ( ) ;
149
162
}
150
163
151
164
updateCachedKeys ( ) : void {
152
165
clearTimeout ( this . timer ) ;
153
-
166
+
154
167
const keys : CachedKey [ ] = [ ] ;
155
168
156
169
this . ngHttpCachingService . getStore ( ) . forEach ( ( value , key ) => {
@@ -170,7 +183,7 @@ export class AppComponent implements OnInit {
170
183
this . ngHttpCachingService . getQueue ( ) . forEach ( ( _ , key ) => {
171
184
keys . push ( { key, headers : [ ] , status : 'queue' } ) ;
172
185
} ) ;
173
- this . cachedKeys = keys ;
186
+ this . cachedKeys . set ( keys ) ;
174
187
175
188
this . timer = setTimeout ( ( ) => this . updateCachedKeys ( ) , 100 ) ;
176
189
}
@@ -179,3 +192,9 @@ export class AppComponent implements OnInit {
179
192
return cachedKey . key + '@' + cachedKey . status ;
180
193
}
181
194
}
195
+
196
+ /**
197
+ * NgHttpCaching demo
198
+ * Cache for HTTP requests in Angular application.
199
+ * see https://www.npmjs.com/package/ng-http-caching
200
+ */
0 commit comments