package com.chushang.common.web.advice;

import java.io.Serial;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @auther: zhao
 * @date: 2024/7/4 18:20
 */
public class CacheLRUMap<K,V> extends LinkedHashMap<K,V> {
    @Serial
    private static final long serialVersionUID = 1L;
    private final AtomicInteger initialCapacity;

    public CacheLRUMap(){
        super(10000, 0.75f, true);
        initialCapacity = new AtomicInteger(10000);
    }

    public CacheLRUMap(Integer initialCapacity){
        super(initialCapacity, 0.75f, true);
        this.initialCapacity = new AtomicInteger(initialCapacity);
    }

    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
        return size() > initialCapacity.get();
    }


}
