package com.njzscloud.common.cache;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.LFUCache;

import java.util.function.Supplier;

@SuppressWarnings("unchecked")
public class FirstCache implements Cache {
    private final LFUCache<String, Object> CACHE;

    public FirstCache(int capacity, long timeout) {
        CACHE = CacheUtil.newLFUCache(capacity, timeout);
        CacheUtil.newNoCache().get("first", null);
    }

    @Override
    public <T> T get(String key) {
        return (T) CACHE.get(key);
    }

    public <T> T get(String key, Supplier<T> supplier) {
        return (T) CACHE.get(key, supplier::get);
    }

    @Override
    public <T> T get(String key, long timeout, Supplier<T> supplier) {
        return (T) CACHE.get(key, true, timeout, supplier::get);
    }

    @Override
    public void put(String key, Object value, long timeout) {
        CACHE.put(key, value, timeout);
    }

    @Override
    public void put(String key, Object value) {
        CACHE.put(key, value);
    }

    @Override
    public void remove(String key) {
        CACHE.remove(key);
    }

}
