2020-10-16 17:26:19 +02:00
|
|
|
// There are plenty of packages for in-memory caching but each have their
|
|
|
|
// own gotchas and often have extra complexity which makes it work in one
|
|
|
|
// platform but not in another (for example, the use of long timeouts would
|
|
|
|
// be fine in Node but not in React Native).
|
|
|
|
//
|
|
|
|
// So this class implements a simple in-memory cache with support for TTL.
|
|
|
|
// Checking for expired keys is a bit inefficient since it doesn't rely on
|
|
|
|
// timers, so it's checking every time a value is set or retrieved. But it
|
|
|
|
// should be fine in most cases, as long as the class is not used at a massive
|
|
|
|
// scale.
|
|
|
|
|
|
|
|
interface Record {
|
2020-11-12 21:29:22 +02:00
|
|
|
value: any;
|
|
|
|
expiredTime: number;
|
2020-10-16 17:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Records {
|
2020-11-12 21:13:28 +02:00
|
|
|
[key: string]: Record;
|
2020-10-16 17:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ExpirableKeys {
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: boolean;
|
2020-10-16 17:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Cache {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private maxRecords_: number;
|
|
|
|
private records_: Records = {};
|
|
|
|
private expirableKeys_: ExpirableKeys = {};
|
|
|
|
private recordKeyHistory_: string[] = [];
|
2020-10-16 17:26:19 +02:00
|
|
|
|
2023-06-30 10:11:26 +02:00
|
|
|
public constructor(maxRecords = 50) {
|
2020-10-16 17:26:19 +02:00
|
|
|
this.maxRecords_ = maxRecords;
|
|
|
|
}
|
|
|
|
|
|
|
|
private checkExpiredRecords() {
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
for (const key in this.expirableKeys_) {
|
|
|
|
if (!this.records_[key]) {
|
|
|
|
delete this.expirableKeys_[key];
|
|
|
|
} else {
|
|
|
|
if (this.records_[key].expiredTime <= now) {
|
|
|
|
delete this.records_[key];
|
|
|
|
delete this.expirableKeys_[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (this.recordKeyHistory_.length > this.maxRecords_) {
|
|
|
|
const key = this.recordKeyHistory_[0];
|
|
|
|
delete this.records_[key];
|
|
|
|
delete this.expirableKeys_[key];
|
|
|
|
this.recordKeyHistory_.splice(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public value(key: string, defaultValue: any = undefined): any {
|
2020-10-16 17:26:19 +02:00
|
|
|
this.checkExpiredRecords();
|
|
|
|
if (key in this.records_) return this.records_[key].value;
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2023-06-30 10:11:26 +02:00
|
|
|
public setValue(key: string, value: any, ttl = 0) {
|
2020-10-16 17:26:19 +02:00
|
|
|
this.checkExpiredRecords();
|
|
|
|
|
|
|
|
this.records_[key] = {
|
|
|
|
value: value,
|
|
|
|
expiredTime: ttl ? Date.now() + ttl : 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
const idx = this.recordKeyHistory_.indexOf(key);
|
|
|
|
if (idx >= 0) this.recordKeyHistory_.splice(idx, 1);
|
|
|
|
this.recordKeyHistory_.push(key);
|
|
|
|
|
|
|
|
if (ttl) {
|
|
|
|
this.expirableKeys_[key] = true;
|
|
|
|
} else {
|
|
|
|
delete this.expirableKeys_[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|