package com.hzya.frame.web.cache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Contentservice方法对应表名称缓存
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-07 11:09
 * @Version 1.0
 */
@Component
public class ServiceMethodCache  {
    Logger logger = LoggerFactory.getLogger(ServiceMethodCache.class);
    private static Map<String, String> serviceMethodMap = new HashMap();

    //查询数据库后 数据添加到缓存
    @Cacheable(key = "'_'+#serviceMethod",cacheNames = "cacheManager:",cacheManager = "cacheManager", unless = "#result == null")
    public String get(String serviceMethod) {
        logger.info("获取：" + serviceMethod);
        return serviceMethodMap.get(serviceMethod);
    }

    //清除一条缓存，key为要清空的数据
    @CacheEvict(cacheNames = "cacheManager", key = "#serviceMethod")
    public void delete(String serviceMethod) {
        serviceMethodMap.remove(serviceMethod);
        logger.info("删除：" + serviceMethod);
    }


    //修改数据后更新缓存
    @CachePut(cacheNames = "cacheManager", key = "'_'+#map['key']", unless = "#result == null")
    public void update(Map<String, String> map) {
        serviceMethodMap.putAll(map);
        logger.info("新增[修改]：" + serviceMethodMap.toString());
    }

}
