package com.uavps.common.core.redis;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author MBQ
 * @Date: 2024/12/31
 * @Description:
 * @Version: 1.0
 **/
@Component
public class SystemCache implements Cache {
    //使用ConcurrentHashMap作为数据的存储
    private Map<String, Object> storage = new ConcurrentHashMap<>();

    @Override
    public String getName() {
        return null;
    }

    @Override
    public Object getNativeCache() {
        return null;
    }

    public boolean hasKey(String key) {
        return storage.containsKey(key);
    }

    @Override
    public ValueWrapper get(Object key) {
        String k = key.toString();
        Object value = storage.get(k);
        return Objects.isNull(value) ? null : new SimpleValueWrapper(value);
    }

    @Override
    public <T> T get(Object key, Class<T> type) {
        return null;
    }

    @Override
    public <T> T get(Object key, Callable<T> valueLoader) {
        return null;
    }

    @Override
    public void put(Object key, Object value) {
        if (Objects.isNull(value)) {
            return;
        }
        storage.put(key.toString(), value);
    }

    @Override
    public void evict(Object key) {
        storage.remove(key.toString());
    }

    public boolean deleteObject(final Collection collection) {
        collection.forEach(o -> {
            storage.remove(o.toString());
        });
        return true;
    }

    public Collection<String> keys(final String pattern) {
        return storage.keySet();
    }

    @Override
    public void clear() {

    }
}
