Recently, I started SpringBoot and wrote down my thoughts on integrating Redis after feeling that SpringBoot is really more convenient than Spring.
Check out the redis dependencies on springBoot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Copy the code
Operating redis
*/ @autoWired StringRedisTemplate stringRedisTemplet; */ @autowired RedisTemplate RedisTemplate;Copy the code
The Redis package provides two operable methods, which can be selected according to different types of values.
The corresponding redis operation is the same for both operations
StringRedisTemplet. OpsForValue () / / string stringRedisTemplet opsForList () / / list stringRedisTemplet. OpsForSet () / / collection StringRedisTemplet. OpsForHash () / / hash stringRedisTemplet opsForZSet () / / ordered setCopy the code
Modify the data storage mode
In StringRedisTemplet, the default is to store a string; In the RedisTemplet, the value can be an object, and redis stores the object in Redis after serialization by default (so stored objects need to be serialized by default).
If you need to change the way the data is stored, for example in REDis using JSON instead of serialized.
1) create a RedisTemplate instance, define your own json serialization format in this instance (org. Springframework. Data. Redis. Serializer. Jackson2JsonRedisSerializer)
/ / here is the incoming employee object (the employee requirement can be serialized) Jackson2JsonRedisSerializer < employee > Jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);Copy the code
2) Put the defined format into your own RedisTemplate instance
RedisTemplate<Object,Employee> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); / / definition format Jackson2JsonRedisSerializer < Employee > Jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class); / / in RedisTemplate example template. SetDefaultSerializer (jackson2JsonRedisSerializer);Copy the code
Reference code:
@Bean
public RedisTemplate<Object,Employee> employeeRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException{
RedisTemplate<Object,Employee> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(jackson2JsonRedisSerializer);
return template;
}
Copy the code
Principle:
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}) // Run public redisTemplate <Object when the container does not currently have redisTemplate, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory);returntemplate; } @bean@conditionalonmissingBean // Run public stringRedisTemplate when the container does not currently have stringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory);returntemplate; }}Copy the code
If you define your own RedisTemplate and add the @bean annotation (to be defined in the configuration class), the default RedisTemplate will not be added to the container and will run your own ReidsTemplate instance, where you define the serialization format. So you define objects in Redis in the format you want.
Change the default buffer
Springboot provides annotation-based caching by default, as long as the main program class (xxxApplication) is marked @enablecaching
@ Cachingable, @ CachingEvict, @ CachingPut and the buffer is used by default ConcurrentHashMapCacheManager
When the starter of Redis is introduced,RedisCache emanager is stored in the container. RedisCache emanager creates RedisCache as a buffer component. RedisCache manipulates redis buffer data
Here I recommend an architecture learning exchange group. Exchange learning group number: 478030634 inside will share some senior architects recorded video video: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, microservice architecture principle, JVM performance optimization, distributed architecture and so on these become architects necessary knowledge system. I can also get free learning resources, which I benefit a lot from now
Modify the serialization mechanism for redis buffers
In SpringBoot, if you want to change the serialization mechanism, you can directly set up a configuration class and customize CacheManager in the configuration class. You can customize serialization rules in CacheManager. The default serialization rules are JDK serialization
Note: Customized CacheManager varies between SpringBoot 1.5.6 and SpringBoot 2.0.5
Reference code:
X version public RedisCacheManager employeeCacheManager(RedisConnectionFactory RedisConnectionFactory){// SpringBoot 1.x public RedisCacheManager employeeCacheManager(RedisConnectionFactory RedisConnectionFactory){// RedisTemplate<Object,Employee> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class); template.setDefaultSerializer(jackson2JsonRedisSerializer); RedisCacheManager RedisCacheManager cacheManager = new RedisCacheManager(template); // 2. cacheManager.setUsePrefix(true); // Will prefix the CacheName keyreturncacheManager; } // springboot 2.x version /** * serializeKeysWith() Serializer() * serializeValuesWith() Here is Jackson2JsonRedisSerializer < Employee > (Employee. Class) * @ param factory * @return
*/
@Bean
public RedisCacheManager employeeCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<Employee>(Employee.class)));
RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).build();
return cacheManager;
}
Copy the code
Tip: you can by looking at the various versions of the org. Springframework. Data. Redis. Cache. RedisCacheConfiguration to custom CacheManager.Copy the code
Different Versions of SpringBoot correspond to different Redis versions. Therefore, you can view the official definition of CacheManager to know how to customize CacheManager.
We think the article is still a little help to you, you can click on the following TWO-DIMENSIONAL code to pay attention to. The “Java Rotten Pigskin” public account is not only about Java technology knowledge, but also about interviews and a lot of architecture. Everyone pay attention to it! Pay attention to rotten pig skin, you will learn more…………..
Original: https://www.cnblogs.com/LxyXY/p/9741921.html