package vip.xiaonuo.coldchain.core.config;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2025/1/6 12:48:37
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class JfcloudRedisCacheService {
    @Autowired
    private CacheManager cacheManager;

    public void evictMonitorTargetRegionCache(String deviceCode, Integer sensorNo) {
        // 获取指定缓存
        Cache cache = cacheManager.getCache(JfcloudColdChainConstants.MONITORTARGETREGION_CACHE_NAME);
        if (cache != null) {
            String key = deviceCode + ":" + sensorNo;
            cache.evict(key);
            log.info("Cache evicted for key: " + key);
        }
    }

    public void evictMonitorTargetCache(String orgId) {
        // 获取指定缓存
        Cache cache = cacheManager.getCache(JfcloudColdChainConstants.MONITORTARGET_CACHE_NAME);
        if (cache != null) {
            cache.evict(orgId);
            log.info("Cache evicted for key: " + orgId);
        }
    }

    public void evictMonitorDeviceCache(){
        // 获取指定缓存
        Cache cache = cacheManager.getCache(JfcloudColdChainConstants.MONITORDEVICE_CACHE_NAME);
        if (cache != null) {
            cache.clear();
            log.info("All caches have been evicted for cache name: " + JfcloudColdChainConstants.MONITORDEVICE_CACHE_NAME);
        }
    }

}

