import NodeCache from "node-cache";

class AppCache {
    private static instance: NodeCache;

    /** Initialize cache at server start */
    static init(options?: NodeCache.Options) {
        if (!AppCache.instance) {
            AppCache.instance = new NodeCache(options);
        }
        return AppCache.instance;
    }

    /** Get cache instance */
    static getInstance(): NodeCache {
        if (!AppCache.instance) {
            throw new Error("AppCache not initialized! Call AppCache.init() at server start.");
        }
        return AppCache.instance;
    }

    /** Helpers */
    static set<T>(key: string, value: T, ttl?: number) {
        return AppCache.getInstance().set(key, value, ttl!);
    }

    static get<T>(key: string): T | undefined {
        return AppCache.getInstance().get<T>(key);
    }

    static del(key: string) {
        return AppCache.getInstance().del(key);
    }
}

export default AppCache;