@@ -70,7 +70,7 @@ export class StorageCache implements ContactCache {
70
70
const isValueStale : boolean = Boolean (
71
71
! cacheItemState ||
72
72
( cacheItemState . state === CacheItemStateType . CACHED &&
73
- now > cacheItemState . updated + this . cacheRefreshIntervalMs )
73
+ now > cacheItemState . timestamp + this . cacheRefreshIntervalMs )
74
74
) ;
75
75
76
76
if ( getFreshValue && isValueStale ) {
@@ -87,12 +87,12 @@ export class StorageCache implements ContactCache {
87
87
return contacts ;
88
88
}
89
89
} catch ( e ) {
90
- this . logErr ( `[${ anonKey } ] Unable to get cache". ` , e ) ;
90
+ this . logErr ( `[${ anonKey } ] Unable to get contacts from cache` , e ) ;
91
91
}
92
92
93
93
if ( ! getFreshValue ) {
94
94
this . log (
95
- `[${ anonKey } ] No getFreshValue function provided - returning empty array`
95
+ `[${ anonKey } ] No " getFreshValue" function provided - returning empty array`
96
96
) ;
97
97
return [ ] ;
98
98
}
@@ -111,13 +111,34 @@ export class StorageCache implements ContactCache {
111
111
}
112
112
}
113
113
114
+ private async setCacheState (
115
+ key : string ,
116
+ state : CacheItemStateType ,
117
+ ttl ?: number
118
+ ) : Promise < void > {
119
+ const anonKey = anonymizeKey ( key ) ;
120
+ this . log ( `[${ anonKey } ] Setting cache state to ${ state } ` ) ;
121
+ try {
122
+ await this . storage . set (
123
+ this . getCacheItemKey ( key ) ,
124
+ {
125
+ timestamp : Date . now ( ) ,
126
+ state,
127
+ } ,
128
+ ttl
129
+ ) ;
130
+ } catch ( error ) {
131
+ this . logErr ( `[${ anonKey } ] Unable to set cache state` , error ) ;
132
+ }
133
+ }
134
+
114
135
public async delete ( key : string ) : Promise < void > {
115
136
const anonKey = anonymizeKey ( key ) ;
116
137
this . log ( `[${ anonKey } ] Removing contacts from cache` ) ;
117
138
try {
118
139
await this . storage . delete ( key ) ;
119
140
} catch ( e ) {
120
- this . logErr ( `[${ anonKey } ] Unable to delete cache` , e ) ;
141
+ this . logErr ( `[${ anonKey } ] Unable to remove contacts from cache` , e ) ;
121
142
}
122
143
}
123
144
@@ -129,11 +150,9 @@ export class StorageCache implements ContactCache {
129
150
130
151
this . log ( `[${ anonKey } ] Setting cache state to FETCHING` ) ;
131
152
132
- await this . storage . set < CacheItemState > (
133
- this . getCacheItemKey ( key ) ,
134
- {
135
- state : CacheItemStateType . FETCHING ,
136
- } ,
153
+ await this . setCacheState (
154
+ key ,
155
+ CacheItemStateType . FETCHING ,
137
156
CACHE_STATE_SECONDS_TTL
138
157
) ;
139
158
@@ -144,10 +163,7 @@ export class StorageCache implements ContactCache {
144
163
145
164
this . log ( `[${ anonKey } ] Setting cache state to CACHED` ) ;
146
165
147
- await this . storage . set < CacheItemState > ( this . getCacheItemKey ( key ) , {
148
- state : CacheItemStateType . CACHED ,
149
- updated : Date . now ( ) ,
150
- } ) ;
166
+ await this . setCacheState ( key , CacheItemStateType . CACHED ) ;
151
167
152
168
return freshValue ;
153
169
} catch ( error ) {
0 commit comments