package com.muyu.cache.utill;

import com.muyu.cache.bean.MyCache;

import java.time.Duration;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;



/**
 * 自定义本地缓存工具类
 */

  public class CacheUtill {

    /**
     * 缓存数据Map
     */
    private static  final Map<String , MyCache> CACHE_MAP = new ConcurrentHashMap<>();

    /**
     * 定时器线程池   用于清理过期缓存
     */
    private static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();


    static {
        // 注册一个定时小鹌鹑线程任务,当服务启动了1秒之后,每隔500毫秒执行一次定时任务
        //定时清理过期缓存
       executorService.scheduleAtFixedRate(CacheUtill::clearCache,1000,500,TimeUnit.MILLISECONDS);

    }

    /**
     * 添加缓存
     * @param key   缓存建
     * @param value
     * @param expire
     */
 public static void put(String key ,Object value,long expire){
     MyCache myCache = new MyCache();
     myCache.setKey(key);
     myCache.setValue(value);
     if (expire > 0 ){
         long expireTime = System.currentTimeMillis() + Duration.ofSeconds(expire).toMillis();
         myCache.setExpireTime(expireTime);
     }
     CACHE_MAP.put(key ,myCache);
 }


    /**
     * 获取缓存
     * @param key  缓存键
     * @return    缓存的数据
     */
 public static Object get(String key){
     if(CACHE_MAP.containsKey(key)){
         return CACHE_MAP.get(key).getValue();
     }
     return null;
 }

    /**
     * 移除缓存
     * @param key
     */
 public static void remove(String key){
     CACHE_MAP.remove(key);
 }


    /**
     * 清理过期的缓存数据
     */
 private static void clearCache(){
     if(CACHE_MAP.size() <= 0){
         return;
     }

     //判断是否过期,过期的话从缓存MAP中删除这个元素
     CACHE_MAP.entrySet().removeIf(entry -> entry.getValue().getExpireTime() != null && entry.getValue().getExpireTime() > System.currentTimeMillis());

 }



}
