4
4
CacheItemStateType ,
5
5
} from "../models/cache-item-state.model" ;
6
6
import { StorageAdapter } from "../models/storage-adapter.model" ;
7
- import { anonymizeKey } from "../util/anonymize-key " ;
7
+ import { errorLogger , infoLogger } from "../util" ;
8
8
9
- const LOG_PREFIX = `[ CACHE]` ;
9
+ const LOG_PREFIX = " CACHE" ;
10
10
const CACHE_STATE_PREFIX = "cache-state:" ;
11
11
const CACHE_STATE_SECONDS_TTL = 1800 ; // 30 minutes
12
12
@@ -24,7 +24,8 @@ export class StorageCache implements ContactCache {
24
24
Math . max ( Number ( CACHE_REFRESH_INTERVAL ) , 1 ) * 1000 ;
25
25
}
26
26
27
- this . log (
27
+ infoLogger (
28
+ LOG_PREFIX ,
28
29
`Initialized storage cache with maximum refresh interval of ${
29
30
this . cacheRefreshIntervalMs / 1000
30
31
} s.`
@@ -35,10 +36,8 @@ export class StorageCache implements ContactCache {
35
36
key : string ,
36
37
getFreshValue ?: ( key : string ) => Promise < Contact [ ] >
37
38
) : Promise < Contact [ ] | CacheItemState > {
38
- const anonKey = anonymizeKey ( key ) ;
39
-
40
39
try {
41
- this . log ( `[ ${ anonKey } ] Trying to get contacts from cache` ) ;
40
+ infoLogger ( LOG_PREFIX , " Trying to get contacts from cache" , key ) ;
42
41
43
42
const cacheItemState = await this . storage . get < CacheItemState > (
44
43
this . getCacheItemKey ( key )
@@ -47,14 +46,18 @@ export class StorageCache implements ContactCache {
47
46
const contacts = await this . storage . get < Contact [ ] > ( key ) ;
48
47
49
48
if ( cacheItemState ?. state === CacheItemStateType . FETCHING ) {
50
- this . log (
51
- `[${ anonKey } ] Not refreshing contacts, because cache state is FETCHING`
49
+ infoLogger (
50
+ LOG_PREFIX ,
51
+ "Not refreshing contacts, because cache state is FETCHING" ,
52
+ key
52
53
) ;
53
54
54
55
// if we have old contacts saved in cache we return them instead
55
56
if ( contacts && contacts ?. length > 0 ) {
56
- this . log (
57
- `[${ anonKey } ] Returning previously cached contacts (${ contacts . length } ), because new contacts are still being fetched`
57
+ infoLogger (
58
+ LOG_PREFIX ,
59
+ `Returning previously cached contacts (${ contacts . length } ), because new contacts are still being fetched` ,
60
+ key
58
61
) ;
59
62
return contacts ;
60
63
}
@@ -63,7 +66,11 @@ export class StorageCache implements ContactCache {
63
66
}
64
67
65
68
if ( contacts ) {
66
- this . log ( `[${ anonKey } ] Found ${ contacts . length } contacts in cache` ) ;
69
+ infoLogger (
70
+ LOG_PREFIX ,
71
+ `Found ${ contacts . length } contacts in cache` ,
72
+ key
73
+ ) ;
67
74
68
75
const now : number = new Date ( ) . getTime ( ) ;
69
76
@@ -74,40 +81,44 @@ export class StorageCache implements ContactCache {
74
81
) ;
75
82
76
83
if ( getFreshValue && isValueStale ) {
77
- this . log (
78
- `[${ anonKey } ] cached value was stale, fetching fresh contacts`
84
+ infoLogger (
85
+ LOG_PREFIX ,
86
+ `Cached value was stale, fetching fresh contacts` ,
87
+ key
79
88
) ;
89
+
80
90
// we don't return the fresh value here because we don't want to wait on the result.
81
91
// We return the old value instead, the fresh value is returned the next time it is requested
82
92
this . getRefreshed ( key , getFreshValue ) . catch ( ( error ) => {
83
- this . logErr ( `[ ${ anonKey } ] Unable to get fresh contacts`, error ) ;
93
+ errorLogger ( LOG_PREFIX , ` Unable to get fresh contacts`, error ) ;
84
94
} ) ;
85
95
}
86
96
87
97
return contacts ;
88
98
}
89
99
} catch ( e ) {
90
- this . logErr ( `[ ${ anonKey } ] Unable to get contacts from cache`, e ) ;
100
+ errorLogger ( LOG_PREFIX , ` Unable to get contacts from cache`, key , e ) ;
91
101
}
92
102
93
103
if ( ! getFreshValue ) {
94
- this . log (
95
- `[${ anonKey } ] No "getFreshValue" function provided - returning empty array`
104
+ infoLogger (
105
+ LOG_PREFIX ,
106
+ `No "getFreshValue" function provided - returning empty array` ,
107
+ key
96
108
) ;
97
109
return [ ] ;
98
110
}
99
111
100
- this . log ( `[ ${ anonKey } ] Found no match in cache. Getting fresh value.` ) ;
112
+ infoLogger ( LOG_PREFIX , ` Found no match in cache. Getting fresh value` , key ) ;
101
113
return this . getRefreshed ( key , getFreshValue ) ;
102
114
}
103
115
104
116
public async set ( key : string , contacts : Contact [ ] ) : Promise < void > {
105
- const anonKey = anonymizeKey ( key ) ;
106
- this . log ( `[${ anonKey } ] Saving ${ contacts . length } contacts to cache` ) ;
117
+ infoLogger ( LOG_PREFIX , `Saving ${ contacts . length } contacts to cache` , key ) ;
107
118
try {
108
119
await this . storage . set ( key , contacts ) ;
109
120
} catch ( e ) {
110
- this . logErr ( `[ ${ anonKey } ] Unable to set cache`, e ) ;
121
+ errorLogger ( LOG_PREFIX , ` Unable to set cache`, key , e ) ;
111
122
}
112
123
}
113
124
@@ -116,8 +127,7 @@ export class StorageCache implements ContactCache {
116
127
state : CacheItemStateType ,
117
128
ttl ?: number
118
129
) : Promise < void > {
119
- const anonKey = anonymizeKey ( key ) ;
120
- this . log ( `[${ anonKey } ] Setting cache state to ${ state } ` ) ;
130
+ infoLogger ( LOG_PREFIX , `Setting cache state to ${ state } ` , key ) ;
121
131
try {
122
132
await this . storage . set (
123
133
this . getCacheItemKey ( key ) ,
@@ -127,28 +137,25 @@ export class StorageCache implements ContactCache {
127
137
} ,
128
138
ttl
129
139
) ;
130
- } catch ( error ) {
131
- this . logErr ( `[ ${ anonKey } ] Unable to set cache state`, error ) ;
140
+ } catch ( e ) {
141
+ errorLogger ( LOG_PREFIX , ` Unable to set cache state`, key , e ) ;
132
142
}
133
143
}
134
144
135
145
public async delete ( key : string ) : Promise < void > {
136
- const anonKey = anonymizeKey ( key ) ;
137
- this . log ( `[${ anonKey } ] Removing contacts from cache` ) ;
146
+ infoLogger ( LOG_PREFIX , `Removing contacts from cache` , key ) ;
138
147
try {
139
148
await this . storage . delete ( key ) ;
140
149
} catch ( e ) {
141
- this . logErr ( `[ ${ anonKey } ] Unable to remove contacts from cache`, e ) ;
150
+ errorLogger ( LOG_PREFIX , ` Unable to remove contacts from cache`, key , e ) ;
142
151
}
143
152
}
144
153
145
154
private async getRefreshed (
146
155
key : string ,
147
156
getFreshValue : ( key : string ) => Promise < Contact [ ] >
148
157
) : Promise < Contact [ ] > {
149
- const anonKey = anonymizeKey ( key ) ;
150
-
151
- this . log ( `[${ anonKey } ] Setting cache state to FETCHING` ) ;
158
+ infoLogger ( LOG_PREFIX , `Setting cache state to FETCHING` , key ) ;
152
159
153
160
await this . setCacheState (
154
161
key ,
@@ -160,37 +167,19 @@ export class StorageCache implements ContactCache {
160
167
const freshValue = await getFreshValue ( key ) ;
161
168
162
169
await this . set ( key , freshValue ) ;
163
-
164
- this . log ( `[${ anonKey } ] Setting cache state to CACHED` ) ;
170
+ infoLogger ( LOG_PREFIX , `Setting cache state to CACHED` , key ) ;
165
171
166
172
await this . setCacheState ( key , CacheItemStateType . CACHED ) ;
167
173
168
174
return freshValue ;
169
- } catch ( error ) {
170
- this . log ( `[ ${ anonKey } ] Error while refreshing value`, error ) ;
175
+ } catch ( e ) {
176
+ errorLogger ( LOG_PREFIX , ` Error while refreshing value`, key , e ) ;
171
177
await this . storage . delete ( this . getCacheItemKey ( key ) ) ;
172
- throw error ;
178
+ throw e ;
173
179
}
174
180
}
175
181
176
182
private getCacheItemKey ( key : string ) {
177
183
return `${ CACHE_STATE_PREFIX } ${ key } ` ;
178
184
}
179
-
180
- private log ( ...args : any ) {
181
- console . log ( this . constructLogMessage ( args ) ) ;
182
- }
183
-
184
- private logErr ( ...args : any ) {
185
- console . error ( this . constructLogMessage ( args ) ) ;
186
- }
187
-
188
- private constructLogMessage ( ...args : any ) {
189
- return `${ LOG_PREFIX } ${ args
190
- . flat ( )
191
- . map ( ( item : unknown ) =>
192
- typeof item !== "string" ? JSON . stringify ( item ) : item
193
- )
194
- . join ( " " ) } `;
195
- }
196
185
}
0 commit comments