千家信息网

JAVA怎么用redisUtils工具类来防止高并发获取缓存出现并发问题

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,本篇内容主要讲解"JAVA怎么用redisUtils工具类来防止高并发获取缓存出现并发问题",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"JAVA怎么用re
千家信息网最后更新 2025年01月23日JAVA怎么用redisUtils工具类来防止高并发获取缓存出现并发问题

本篇内容主要讲解"JAVA怎么用redisUtils工具类来防止高并发获取缓存出现并发问题",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"JAVA怎么用redisUtils工具类来防止高并发获取缓存出现并发问题"吧!

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;@Slf4jpublic 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 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  redisTemplate, String key, Function 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 redisTemplate, String key, String hashKey, BiFunction 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 redisTemplate, String key, Function> 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 mappingFunction){        BoundHashOperations 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;    }}

到此,相信大家对"JAVA怎么用redisUtils工具类来防止高并发获取缓存出现并发问题"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0