/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see .
*/
interface CacheEntry {
value: T;
expiresAt: number | null;
}
export class InMemoryCacheService {
private cache = new Map>();
async get(key: string): Promise {
const entry = this.cache.get(key);
if (!entry) {
return null;
}
if (entry.expiresAt && entry.expiresAt < Date.now()) {
this.cache.delete(key);
return null;
}
return entry.value as T;
}
async set(key: string, value: T, ttlSeconds?: number): Promise {
const entry: CacheEntry = {
value,
expiresAt: ttlSeconds ? Date.now() + ttlSeconds * 1000 : null,
};
this.cache.set(key, entry as CacheEntry);
}
async delete(key: string): Promise {
this.cache.delete(key);
}
}