本篇内容主要讲解“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; @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; } }
到此,相信大家对“JAVA怎么用redisUtils工具类来防止高并发获取缓存出现并发问题”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。