nuxt-render-cache
Version:
Powerful caching library for Nuxt 3 applications. Solves SSR performance issues with flexible caching system supporting TTL, locks, and distributed servers. Cache Vue components and async data with soft/hard TTL strategy.
83 lines (82 loc) • 1.77 kB
JavaScript
export class CacheEntity {
value;
ttlSoft;
ttlHard;
locked;
expiresSoftAt;
expiresHardAt;
constructor(value, ttlSoft, ttlHard, expiresSoftAt = Date.now() + ttlSoft, expiresHardAt = Date.now() + ttlHard, locked = false) {
if (ttlSoft <= 0 || ttlHard <= 0) {
throw new Error("TTL values must be positive numbers");
}
if (ttlSoft > ttlHard) {
throw new Error("Soft TTL must be less than or equal to hard TTL");
}
this.value = value;
this.ttlSoft = ttlSoft;
this.ttlHard = ttlHard;
this.locked = locked;
this.expiresSoftAt = expiresSoftAt;
this.expiresHardAt = expiresHardAt;
}
// Getters for read-only properties
get ttlSoftValue() {
return this.ttlSoft;
}
get ttlHardValue() {
return this.ttlHard;
}
get isExpiredSoft() {
return this.isSoftExpired();
}
get isExpiredHard() {
return this.isHardExpired();
}
get expiresAtSoft() {
return this.expiresSoftAt;
}
get expiresAtHard() {
return this.expiresHardAt;
}
isSoftExpired() {
return Date.now() > this.expiresSoftAt;
}
isHardExpired() {
return Date.now() > this.expiresHardAt;
}
isLocked() {
return this.locked;
}
lock() {
this.locked = true;
}
unlock() {
this.locked = false;
}
getValue() {
return this.value;
}
getValueAs() {
return this.value;
}
toJson() {
return {
value: this.value,
ttlSoft: this.ttlSoft,
ttlHard: this.ttlHard,
expiresSoftAt: this.expiresSoftAt,
expiresHardAt: this.expiresHardAt,
locked: this.locked
};
}
toArray() {
return [
this.value,
this.ttlSoft,
this.ttlHard,
this.expiresSoftAt,
this.expiresHardAt,
this.locked
];
}
}