JAVA 编写redisUtils工具类,防止高并发获取缓存出现并发问题

原创
2019/10/23 20:37
阅读数 2.1K
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;
    }
}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部