How does JAVA use redisUtils utility classes to prevent concurrency problems in highly concurrent access caches?
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)