package vip.xiaonuo.coldchain.core.cache.monitordevice.impl;

import org.springframework.stereotype.Component;
import vip.xiaonuo.coldchain.core.cache.monitordevice.MonitorDeviceCacheService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2024/11/19 17:36:25
 */

/**
 * 本地缓存实现类
 */
@Component
public class LocalMonitorDeviceCacheService implements MonitorDeviceCacheService {
    private final Map<String, String> localCacheMap = new ConcurrentHashMap<>();

    @Override
    public void cacheDevice(String deviceCode, String deviceModel) {
        localCacheMap.put(deviceCode, deviceModel);
    }

    @Override
    public String getDeviceModel(String deviceCode) {
        return localCacheMap.get(deviceCode);
    }

    @Override
    public boolean exists(String deviceCode) {
        return localCacheMap.containsKey(deviceCode);
    }

    @Override
    public void removeDevice(String deviceCode) {
        localCacheMap.remove(deviceCode);
    }

    @Override
    public void removeBatch(String pattern) {
        localCacheMap.entrySet().removeIf(entry -> entry.getKey().startsWith(pattern));
    }

    @Override
    public Set<String> getAllDeviceCodes() {
        return localCacheMap.keySet();
    }

    @Override
    public List<String> getAllDeviceModels() {
        return new ArrayList<>(localCacheMap.values());
    }
}
