Skip to content

Commit 883eb9a

Browse files
committed
Use interface instead of separate class
1 parent 53dc62b commit 883eb9a

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

packages/shared/akamai-edgeworker-sdk/src/featureStore/cache-item.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { CacheItem } from './cache-item';
1+
interface CacheItem {
2+
value: any,
3+
expiration: number,
4+
}
25

36
export default class Cache {
4-
cache: CacheItem | undefined;
5-
cachedAt: number | undefined;
7+
_cache: CacheItem | undefined;
68

79
constructor(private readonly _cacheTtlMs: number) {}
810

@@ -13,26 +15,29 @@ export default class Cache {
1315
}
1416

1517
// If there isn't a cached item, we must return undefined.
16-
if (this.cache === undefined) {
18+
if (this._cache === undefined) {
1719
return undefined;
1820
}
1921

2022
// A cacheTtlMs of 0 is infinite caching, so we can always return the
2123
// value.
2224
//
23-
// We also want to return it if the cache is still considered fresh.
24-
if (this._cacheTtlMs === 0 || this.cache.fresh(this._cacheTtlMs)) {
25-
return this.cache.value;
25+
// We also want to return the value if it hasn't expired.
26+
if (this._cacheTtlMs === 0 || Date.now() < this._cache.expiration) {
27+
return this._cache.value;
2628
}
2729

2830
// If you have gotten this far, the cache is stale. Better to drop it as a
2931
// way to short-circuit checking the freshness again.
30-
this.cache = undefined;
32+
this._cache = undefined;
3133

3234
return undefined;
3335
}
3436

3537
set(value: any): void {
36-
this.cache = new CacheItem(value);
38+
this._cache = {
39+
value: value,
40+
expiration: Date.now() + this._cacheTtlMs,
41+
}
3742
}
3843
}

packages/shared/akamai-edgeworker-sdk/src/featureStore/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,25 @@ export class EdgeFeatureStore implements LDFeatureStore {
9696
// This method is used to retrieve the environment payload from the edge
9797
// provider. It will cache the payload for the duration of the cacheTtlMs.
9898
private async _getStorePayload(): Promise<ReturnType<typeof deserializePoll>> {
99-
let item = this._cache.get();
100-
if (item !== undefined) {
101-
return item;
99+
let payload = this._cache.get();
100+
if (payload !== undefined) {
101+
return payload;
102102
}
103103

104-
const i = await this._edgeProvider.get(this._rootKey);
104+
const providerData = await this._edgeProvider.get(this._rootKey);
105105

106-
if (!i) {
106+
if (!providerData) {
107107
throw new Error(`${this._rootKey} is not found in KV.`);
108108
}
109109

110-
item = deserializePoll(i);
111-
if (!item) {
110+
payload = deserializePoll(providerData);
111+
if (!payload) {
112112
throw new Error(`Error deserializing ${this._rootKey}`);
113113
}
114114

115-
this._cache.set(item);
115+
this._cache.set(payload);
116116

117-
return item;
117+
return payload;
118118
}
119119

120120
async initialized(callback: (isInitialized: boolean) => void = noop): Promise<void> {

0 commit comments

Comments
 (0)