@@ -18,6 +18,7 @@ export class StorageCache implements ContactCache {
18
18
this . storage = storageAdapter ;
19
19
20
20
const { CACHE_REFRESH_INTERVAL } = process . env ;
21
+
21
22
if ( CACHE_REFRESH_INTERVAL ) {
22
23
this . cacheRefreshIntervalMs =
23
24
Math . max ( Number ( CACHE_REFRESH_INTERVAL ) , 1 ) * 1000 ;
@@ -34,49 +35,35 @@ export class StorageCache implements ContactCache {
34
35
key : string ,
35
36
getFreshValue ?: ( key : string ) => Promise < Contact [ ] >
36
37
) : Promise < Contact [ ] | CacheItemState > {
38
+ const anonKey = anonymizeKey ( key ) ;
39
+
37
40
try {
38
- this . log ( `[${ anonymizeKey ( key ) } ] Trying to get Contacts from cache… ` ) ;
39
- const start = performance . now ( ) ;
41
+ this . log ( `[${ anonKey } ] Trying to get contacts from cache` ) ;
42
+
40
43
const cacheItemState = await this . storage . get < CacheItemState > (
41
44
this . getCacheItemKey ( key )
42
45
) ;
43
46
44
- const value = await this . storage . get < Contact [ ] > ( key ) ;
45
- console . log (
46
- `[${ anonymizeKey ( key ) } ] loading contacts took ${
47
- performance . now ( ) - start
48
- } ms`
49
- ) ;
47
+ const contacts = await this . storage . get < Contact [ ] > ( key ) ;
50
48
51
- if (
52
- cacheItemState &&
53
- cacheItemState . state === CacheItemStateType . FETCHING
54
- ) {
49
+ if ( cacheItemState ?. state === CacheItemStateType . FETCHING ) {
55
50
this . log (
56
- `[${ anonymizeKey (
57
- key
58
- ) } ] Not refreshing for because fetching is already in progress.`
51
+ `[${ anonKey } ] Not refreshing contacts, because cache state is FETCHING`
59
52
) ;
60
53
61
54
// if we have old contacts saved in cache we return them instead
62
- if ( value && value . length > 0 ) {
55
+ if ( contacts && contacts ? .length > 0 ) {
63
56
this . log (
64
- `[${ anonymizeKey ( key ) } ] Returning previously cached contacts (${
65
- value . length
66
- } ), because new contacts are still being fetched.`
57
+ `[${ anonKey } ] Returning previously cached contacts (${ contacts . length } ), because new contacts are still being fetched`
67
58
) ;
68
- return value ;
59
+ return contacts ;
69
60
}
70
61
71
62
return cacheItemState ;
72
63
}
73
64
74
- if ( value ) {
75
- this . log (
76
- `[${ anonymizeKey ( key ) } ] Found contacts ${
77
- value . length
78
- } for key in cache.`
79
- ) ;
65
+ if ( contacts ) {
66
+ this . log ( `[${ anonKey } ] Found ${ contacts . length } contacts in cache` ) ;
80
67
81
68
const now : number = new Date ( ) . getTime ( ) ;
82
69
@@ -88,65 +75,59 @@ export class StorageCache implements ContactCache {
88
75
89
76
if ( getFreshValue && isValueStale ) {
90
77
this . log (
91
- `[${ anonymizeKey ( key ) } ] value was stale, fetching fresh contacts`
78
+ `[${ anonKey } ] cached value was stale, fetching fresh contacts`
92
79
) ;
93
80
// we don't return the fresh value here because we don't want to wait on the result.
94
81
// We return the old value instead, the fresh value is returned the next time it is requested
95
82
this . getRefreshed ( key , getFreshValue ) . catch ( ( error ) => {
96
- this . logErr (
97
- `[${ anonymizeKey (
98
- key
99
- ) } ] Unable to get fresh values, error was ${ error } `
100
- ) ;
83
+ this . logErr ( `[${ anonKey } ] Unable to get fresh contacts` , error ) ;
101
84
} ) ;
102
85
}
103
86
104
- return value ;
87
+ return contacts ;
105
88
}
106
89
} catch ( e ) {
107
- this . logErr ( `[${ anonymizeKey ( key ) } ] Unable to get cache".` , e ) ;
90
+ this . logErr ( `[${ anonKey } ] Unable to get cache".` , e ) ;
108
91
}
109
92
110
93
if ( ! getFreshValue ) {
111
94
this . log (
112
- `[${ anonymizeKey (
113
- key
114
- ) } ] No getFreshValue function provided. Returning empty array.`
95
+ `[${ anonKey } ] No getFreshValue function provided - returning empty array`
115
96
) ;
116
97
return [ ] ;
117
98
}
118
99
119
- this . log (
120
- `[${ anonymizeKey ( key ) } ] Found no match in cache. Getting fresh value.`
121
- ) ;
100
+ this . log ( `[${ anonKey } ] Found no match in cache. Getting fresh value.` ) ;
122
101
return this . getRefreshed ( key , getFreshValue ) ;
123
102
}
124
103
125
- public async set ( key : string , value : Contact [ ] ) : Promise < void > {
126
- this . log (
127
- `[${ anonymizeKey ( key ) } ] Saving ${ value . length } contacts to cache.`
128
- ) ;
104
+ public async set ( key : string , contacts : Contact [ ] ) : Promise < void > {
105
+ const anonKey = anonymizeKey ( key ) ;
106
+ this . log ( `[${ anonKey } ] Saving ${ contacts . length } contacts to cache` ) ;
129
107
try {
130
- await this . storage . set ( key , value ) ;
108
+ await this . storage . set ( key , contacts ) ;
131
109
} catch ( e ) {
132
- this . logErr ( `[${ anonymizeKey ( key ) } ] Unable to set cache. ` , e ) ;
110
+ this . logErr ( `[${ anonKey } ] Unable to set cache` , e ) ;
133
111
}
134
112
}
135
113
136
114
public async delete ( key : string ) : Promise < void > {
137
- this . log ( `[${ anonymizeKey ( key ) } ] Removing contacts from cache.` ) ;
115
+ const anonKey = anonymizeKey ( key ) ;
116
+ this . log ( `[${ anonKey } ] Removing contacts from cache` ) ;
138
117
try {
139
118
await this . storage . delete ( key ) ;
140
119
} catch ( e ) {
141
- this . logErr ( `[${ anonymizeKey ( key ) } ] Unable to delete cache` , e ) ;
120
+ this . logErr ( `[${ anonKey } ] Unable to delete cache` , e ) ;
142
121
}
143
122
}
144
123
145
124
private async getRefreshed (
146
125
key : string ,
147
126
getFreshValue : ( key : string ) => Promise < Contact [ ] >
148
127
) : Promise < Contact [ ] > {
149
- this . log ( `[${ anonymizeKey ( key ) } ] Refreshing value…` ) ;
128
+ const anonKey = anonymizeKey ( key ) ;
129
+
130
+ this . log ( `[${ anonKey } ] Setting cache state to FETCHING` ) ;
150
131
151
132
await this . storage . set < CacheItemState > (
152
133
this . getCacheItemKey ( key ) ,
@@ -159,13 +140,19 @@ export class StorageCache implements ContactCache {
159
140
try {
160
141
const freshValue = await getFreshValue ( key ) ;
161
142
162
- if ( freshValue ) {
163
- await this . set ( key , freshValue ) ;
164
- }
143
+ await this . set ( key , freshValue ) ;
144
+
145
+ this . log ( `[${ anonKey } ] Setting cache state to CACHED` ) ;
146
+
147
+ await this . storage . set < CacheItemState > ( this . getCacheItemKey ( key ) , {
148
+ state : CacheItemStateType . CACHED ,
149
+ updated : Date . now ( ) ,
150
+ } ) ;
165
151
166
152
return freshValue ;
167
153
} catch ( error ) {
168
- this . log ( `[${ anonymizeKey ( key ) } ] Error while refreshing value` , error ) ;
154
+ this . log ( `[${ anonKey } ] Error while refreshing value` , error ) ;
155
+ await this . storage . delete ( this . getCacheItemKey ( key ) ) ;
169
156
throw error ;
170
157
}
171
158
}
0 commit comments