In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to solve the failure of SpringBoot using RedisTemplate.delete to delete the specified key". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Overview of the problem
With regard to this problem, it still mainly appears in the management of project development. First, describe the project environment:
1. JDK-1.8 used by Java
2. Spring Boot-2.0.3 used by Spring Boot
3. Redis uses 2.0.8
4. Jedis uses 2.9.0
(of course, nothing else will be described.)
At first, native ReidsTemplate API was used to manage the system cache. Later, because it involves object-related operations, if you still use native API, the object in the cache is a dazzling mess, which requires a custom serialization mechanism, and the colleague added it silently, without any feedback from the team.
Of course, in the management and operation of the system cache, the blogger is still calmly using RedisTemplate's native API, and then a day ago, the test team suddenly exploded, all kinds of problems.
Phenomenon: after deleting the Key in the cache through the native API of Redis, the system does not have any error message and indicates that the deletion is successful, and then go to the Redis server to see that the Key that should be deleted still exists.
Solution.
After a period of arduous BUG inspection, coupled with various postures to check the Redis source code, the problem is finally solved. Paste a Redis source code as follows:
Public void afterPropertiesSet () {super.afterPropertiesSet (); boolean defaultUsed = false; if (this.defaultSerializer = = null) {this.defaultSerializer = new JdkSerializationRedisSerializer (this.classLoader! = null? This.classLoader: this.getClass (). GetClassLoader ();} if (this.enableDefaultSerializer) {if (this.keySerializer = = null) {this.keySerializer = this.defaultSerializer; defaultUsed = true;} if (this.valueSerializer = = null) {this.valueSerializer = this.defaultSerializer; defaultUsed = true } if (this.hashKeySerializer = = null) {this.hashKeySerializer = this.defaultSerializer; defaultUsed = true;} if (this.hashValueSerializer = = null) {this.hashValueSerializer = this.defaultSerializer; defaultUsed = true } if (this.enableDefaultSerializer & & defaultUsed) {Assert.notNull (this.defaultSerializer, "default serializer null and not all serializers initialized");} if (this.scriptExecutor = = null) {this.scriptExecutor = new DefaultScriptExecutor (this);} this.initialized = true;}
"Redis source address"
Note Redis's default serialization mechanism "defaultSerializer". If there is no custom serialization mechanism, the system uses "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" by default, and then Lao Tie silently customizes the Redis serialization mechanism in the system. Part of the sample code is as follows:
Package com.btc.common.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer Import org.springframework.data.redis.serializer.StringRedisSerializer; @ Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport {@ Bean public CacheManager cacheManager final RedisConnectionFactory redisConnectionFactory) {RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory (redisConnectionFactory); return builder.build ();} @ Bean (name = "springSessionDefaultRedisSerializer") public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer () {return new GenericJackson2JsonRedisSerializer () } @ Bean @ ConditionalOnMissingBean (name = "redisTemplate") public RedisTemplate getRedisTemplate (final RedisConnectionFactory connectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setConnectionFactory (connectionFactory); / / configure the default serializer redisTemplate.setDefaultSerializer (getGenericJackson2JsonRedisSerializer ()); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer () / / set the default serialization mechanism of Key: redisTemplate.setKeySerializer (stringRedisSerializer); redisTemplate.setHashKeySerializer (stringRedisSerializer); return redisTemplate;}}
In this code, the serialization mechanism of Redis Key is changed to "org.springframework.data.redis.serializer.StringRedisSerializer" according to the requirements (the main purpose is to avoid the occurrence of garbled quadrants, but it can still be used normally, but we can see it as garbled with the naked eye), and redisTemplate is defined as a generic type.
This is where the problem lies. Instead of using native "RedisTemplate redisTemplate;", you need to define "RedisTemplate redisTemplate" as generic. "Yes, because when we add key again, we use the" StringRedisSerializer "serialization mechanism, but when we operate on delete, we use native API, and the default serialization mechanism of redisTemplate in redis uses" JdkSerializationRedisSerializer ". So, even if you use the hasKey method, you will find that this key exists in redis, but the actual hasKey returns false, so it will be deleted successfully. But the actual data is still on the Redis server.
This is the end of the content of "SpringBoot uses RedisTemplate.delete to delete and specify how to solve key failure". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.