This article introduces the methods and steps of using redis cache in SpringBoot project. I will share it with you. The details are as follows:
Spring Data Redis encapsulates various operations of the Redis client for us to simplify use.
- When Redis is used as a database or message queue, we generally use RedisTemplate to operate
- When Redis is used as a cache, we can use it as an implementation of Spring Cache, directly using it through annotation
1. Overview
Effectively utilizing redis cache in applications can improve system performance well, especially for query operations, which can effectively reduce database pressure.
For specific code, refer to this example project
2. Add a reference
Join in build.gradle
compile('org.springframework.boot:spring-boot-starter-data-redis')SpringBoot will automatically introduce redis-related jar packages. After adding this reference, you need to install redis locally and start it, otherwise an error will be reported when the program starts.
3. Enable cache through annotations
It is very simple to enable redis in SpringBoot. You only need to add the @EnableCaching annotation on the Application main class, and then add the @Cacheable annotation on the query method that needs to enable cache.
@SpringBootApplication@EnableCachingpublic class DemoApplication implements CommandLineRunner{...Query interface:
public interface TestRepository extends JpaRepository<Test, Integer> { @Cacheable(value = "testCache") public Test findOne(Integer id);}Entity classes need to implement the Serializable interface, otherwise the program will report an error because the Java object cannot be serialized into redis. Redis in SpringBoot uses DefaultSerializer by default, which uses jdk's own serialization method.
There are a total of the following serialization methods. For specific usage scenarios, please refer to the official documentation.
1. GenericJackson2JsonRedisSerializer
2. GenericToStringSerializer
3. Jackson2JsonRedisSerializer
4. JacksonJsonRedisSerializer
5. JdkSerializationRedisSerializer
6. OxmSerializer
7. StringRedisSerializer
At this point, our program has the ability to query data from the redis cache. If you don’t mind the aesthetics of the KEY stored in redis, the work will be over.
4. Beautiful KEY
After executing our program, execute the KEY * command in redis-cli and you will find that the value of the key is a bunch of things similar to garbled code:
"testCache:/xac/xed/x00/x05sr/x00/x11java.lang.Integer/x12/xe2/xa0/xa4/xf7/x81/x878/x02/x00/x01I/x00/x05valueexr/x00/x10java.lang.Number/x86/xac/x95/x1d/x0b/x94/xe0/x8b/x02/x00/x00xp/x00/x00/x00/x01"
The key value in this is probably unacceptable to the operation and maintenance personnel of Redis. We need to find a way to make the key value look better, at least to make people understand it.
The reason for the above key value is that the SimpleKey class is used by default in spring to generate the redis key.
The solution is also very simple. Add cache configuration and specify how redis generates keys:
@Configurationpublic class CacheConfig extends CachingConfigurerSupport { @Autowired private RedisTemplate redisTemplate; @Bean public CacheManager cacheManager() { redisTemplate.setKeySerializer(new GenericToStringSerializer<Object>(Object.class)); RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(3600); cacheManager.setUsePrefix(true); cacheManager.setCachePrefix(new RedisCachePrefix() { private final RedisSerializer<String> serializer = new StringRedisSerializer(); private final String delimiter = ":"; public byte[] prefix(String cacheName) { return this.serializer .serialize(cacheName.concat(this.delimiter)); } }); return cacheManager; }}in
The code copy is as follows:
redisTemplate.setKeySerializer(new GenericToStringSerializer<Object>(Object.class));
This line of code specifies the generation method of key values in redis. The serialization method of GenericToStringSerializer will convert the java object into a string and store it in redis.
5. Summary
Enabling redis cache in SpringBoot is very simple, you only need to add a few annotations. At the same time, we can add cache configuration to make the key values stored in redis have good readability, rather than a bunch of garbled data.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.