Skip to content

Commit 3ed5274

Browse files
committed
🔊 cache state logging
1 parent 051d914 commit 3ed5274

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

‎src/cache/storage-cache.ts‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class StorageCache implements ContactCache {
7070
const isValueStale: boolean = Boolean(
7171
!cacheItemState ||
7272
(cacheItemState.state === CacheItemStateType.CACHED &&
73-
now > cacheItemState.updated + this.cacheRefreshIntervalMs)
73+
now > cacheItemState.timestamp + this.cacheRefreshIntervalMs)
7474
);
7575

7676
if (getFreshValue && isValueStale) {
@@ -87,12 +87,12 @@ export class StorageCache implements ContactCache {
8787
return contacts;
8888
}
8989
} catch (e) {
90-
this.logErr(`[${anonKey}] Unable to get cache".`, e);
90+
this.logErr(`[${anonKey}] Unable to get contacts from cache`, e);
9191
}
9292

9393
if (!getFreshValue) {
9494
this.log(
95-
`[${anonKey}] No getFreshValue function provided - returning empty array`
95+
`[${anonKey}] No "getFreshValue" function provided - returning empty array`
9696
);
9797
return [];
9898
}
@@ -111,13 +111,34 @@ export class StorageCache implements ContactCache {
111111
}
112112
}
113113

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+
114135
public async delete(key: string): Promise<void> {
115136
const anonKey = anonymizeKey(key);
116137
this.log(`[${anonKey}] Removing contacts from cache`);
117138
try {
118139
await this.storage.delete(key);
119140
} catch (e) {
120-
this.logErr(`[${anonKey}] Unable to delete cache`, e);
141+
this.logErr(`[${anonKey}] Unable to remove contacts from cache`, e);
121142
}
122143
}
123144

@@ -129,11 +150,9 @@ export class StorageCache implements ContactCache {
129150

130151
this.log(`[${anonKey}] Setting cache state to FETCHING`);
131152

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,
137156
CACHE_STATE_SECONDS_TTL
138157
);
139158

@@ -144,10 +163,7 @@ export class StorageCache implements ContactCache {
144163

145164
this.log(`[${anonKey}] Setting cache state to CACHED`);
146165

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);
151167

152168
return freshValue;
153169
} catch (error) {

‎src/models/cache-item-state.model.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ export enum CacheItemStateType {
33
FETCHING = "FETCHING",
44
}
55

6-
export interface CacheItemStateCached {
7-
state: CacheItemStateType.CACHED;
8-
updated: number;
9-
}
10-
11-
export interface CacheItemStateFetching {
12-
state: CacheItemStateType.FETCHING;
13-
}
14-
15-
export type CacheItemState = CacheItemStateCached | CacheItemStateFetching;
6+
export type CacheItemState = {
7+
state: CacheItemStateType;
8+
timestamp: number;
9+
};

0 commit comments

Comments
 (0)