您好,登錄后才能下訂單哦!
本篇內容主要講解“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。