@ynotzort/sveltekit-password-protect
Version:
Simple utility to add a layer of protection to your websites, very useful for agencies and freelancers
27 lines (26 loc) • 774 B
JavaScript
// async set, get, delete so other memory adapters like redis or once what can not do sync operations can be used
export class InMemoryCache {
cache = new Map();
timeouts = new Map();
defaultTTL = 30 * 24 * 60 * 60 * 1000; // 30 days
constructor(defaultTTL) {
if (defaultTTL) {
this.defaultTTL = defaultTTL;
}
}
async set(key, value, ttl) {
this.cache.set(key, value);
ttl = ttl || this.defaultTTL;
const timeOut = setTimeout(() => {
this.delete(key);
}, ttl);
this.timeouts.set(key, timeOut);
}
async get(key) {
return this.cache.get(key) ?? null;
}
async delete(key) {
this.cache.delete(key);
this.timeouts.delete(key);
}
}