package com.ruoyi.common.third.cache;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.lang.Nullable;

import java.time.Duration;
import java.util.List;
import java.util.Map;

public interface HzCache extends Cache {

    /**
     * 过期存储
     *
     * @param key     索引
     * @param value   值
     * @param timeout 过期时间
     */
    void put(Object key, Object value, Duration timeout);
    /**
     * 过期存储
     *
     * @param key     索引
     * @param value   值
     * @param seconds 过期时间 秒
     */
    void put(Object key, Object value, long seconds);
    /**
     * 不存在 放入
     * 存在 获取之前的值
     *
     * @return ValueWrapper
     * */
    default ValueWrapper putIfAbsent(Object key, @Nullable Object value){
        ValueWrapper existingValue = get(key);
        if (existingValue == null) {
            put(key, value);
            existingValue = new SimpleValueWrapper(value);
        }
        return existingValue;
    }

    default ValueWrapper putIfAbsent(Object key, @Nullable Object value,Duration timeout){
        ValueWrapper existingValue = get(key);
        if (existingValue == null) {
            put(key, value, timeout);
            existingValue = new SimpleValueWrapper(value);
        }
        return existingValue;
    }

    default ValueWrapper putIfAbsent(Object key, @Nullable Object value,long seconds){
        Duration timeoutDuration;
        if (seconds > 0) {
            timeoutDuration = Duration.ofSeconds(seconds);
        }else{
            timeoutDuration = Duration.ZERO;
        }
        ValueWrapper existingValue = get(key);
        if (existingValue == null) {
            this.put(key,value,timeoutDuration);
            existingValue = new SimpleValueWrapper(value);
        }
        return existingValue;
    }
    /**
     * 判断
     *
     * @param key 索引
     * @return 是否
     */
    boolean containKey(Object key);

    /**
     * 模糊匹配获取key
     * */
    List<String> getKeys(String key);

    /**
     * 模糊匹配获取key
     * */
    List<String> scanKeys(String key);
    /**
     * 模糊匹配删除key
     * */
    void evictKeys(String key);

    /***
     * 根据key模糊匹配 获取key + value的list<map>的列表
     *
     * @return*/
    Map<String, Object> getKeysAndValues(String key);

    Long getKeyExpireTime(Object key);


}
