import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Function;

@Slf4j
public class RedisUtils{

    /**
     * @param redisTemplate redis操作模板工具
     * @param key           缓存主键
     * @param timeout       过期时间
     * @param timeUnit      过期时间单位
     * @return
     * @description 方法描述
     * @author kejie.huang
     * @date 2019/10/18 9:20
     */
    public static void expire(RedisTemplate<Object, Object> redisTemplate, String key, long timeout, TimeUnit timeUnit) {
        boolean hasKey = redisTemplate.hasKey(key);
        // 如果key存在
        if (hasKey) {
            // 获取过期时间是永久则设置
            Long timeOut = redisTemplate.getExpire(key);
            if (timeOut.intValue() == -1) {
                redisTemplate.expire(key, timeout, timeUnit);
            }
        }
    }

    public static Object getValue(RedisTemplate<Object, Object>  redisTemplate, String key, Function<String, Object> mappingFunction){
        BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key);
        Object value = boundValueOperations.get();
        if(value == null){
            synchronized (key.intern()){
                value =  boundValueOperations.get();
                if(value == null){
                    value = mappingFunction.apply(key);
                    if(value != null){
                        boundValueOperations.set(value);
                    }
                }
            }
        }
        return value;
    }

    public static Object getHashValue(RedisTemplate<Object, Object> redisTemplate, String key, String hashKey, BiFunction<String, String, Object> mappingFunction){
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key);
        Object value = boundHashOperations.get(hashKey);
        if(value == null){
            synchronized (key.concat(hashKey).intern()){
                value =  boundHashOperations.get(hashKey);
                if(value == null){
                    value = mappingFunction.apply(key, hashKey);
                    if(value != null){
                        boundHashOperations.put(hashKey, value);
                    }
                }
            }
        }
        return value;
    }

    public static Map<?, ?> getHashValue(RedisTemplate<Object, Object> redisTemplate, String key, Function<String, Map<?,?>> mappingFunction){
        Map<?,?> entries = redisTemplate.opsForHash().entries(key);
        if(entries == null){
            synchronized (key.intern()){
                if(!redisTemplate.hasKey(key)){
                    entries = mappingFunction.apply(key);
                    if(entries != null){
                        redisTemplate.opsForHash().putAll(key, entries);
                    }
                }
            }
        }
        return entries;
    }

    public static String getHashValue(StringRedisTemplate redisTemplate, String key, String hashKey, BiFunction<String, String, String> mappingFunction){
        BoundHashOperations<String, String, String> boundHashOperations = redisTemplate.boundHashOps(key);
        String value = boundHashOperations.get(hashKey);
        if(value == null){
            synchronized (key.concat(hashKey).intern()){
                value =  boundHashOperations.get(hashKey);
                if(value == null){
                    value = mappingFunction.apply(key, hashKey);
                    if(value != null){
                        boundHashOperations.put(hashKey, value);
                    }
                }
            }
        }
        return value;
    }
}
10-24 01:30