In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "JAVA how to use the redisUtils tool class to prevent high concurrency access cache concurrency problems", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how JAVA uses the redisUtils utility class to prevent concurrency problems in highly concurrent access caches."
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 operation template tool * @ param key cache primary key * @ param timeout expiration time * @ param timeUnit expiration time unit * @ return * @ description method description * @ author kejie.huang * @ date 9:20 on 2019-10-18 * / public static void expire (RedisTemplate redisTemplate, String key Long timeout, TimeUnit timeUnit) {boolean hasKey = redisTemplate.hasKey (key) / / if key exists if (hasKey) {/ / sets Long timeOut = redisTemplate.getExpire (key) if the expiration time is permanent; 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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.