In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to configure a single point of Redis cache in SpringBoot. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.interceptor.KeyGenerator;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.core.StringRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import java.lang.reflect.Method;/** * redis cache configuration; *
* Note: RedisCacheConfig does not have to inherit here: CachingConfigurerSupport, that is, a normal Class is fine. *
* the main thing here is that we need to re-implement the generation strategy of key. As long as we modify the KeyGenerator here, other locations will take effect without modification. *
* if you usually use a normal class, you need to specify the name of KeyGenerator when using @ Cacheable; this is troublesome to encode. * * @ create 2017-12-13 12:45 * * / @ Configuration@EnableCaching// enables caching, which is important; public class RedisCacheConfig extends CachingConfigurerSupport {/ * cache manager. * * @ param redisTemplate * @ return * / @ Bean public CacheManager cacheManager (RedisTemplate redisTemplate) {RedisCacheManager cacheManager = new RedisCacheManager (redisTemplate); / / set cache expiration time cacheManager.setDefaultExpiration (10000); return cacheManager;} / * redis template operation class, similar to a class of jdbcTemplate;
* although CacheManager can also obtain Cache objects, it is not so flexible to operate. *
* here under the extension: RedisTemplate this class is not very easy to operate, we can extend one of our *
* your own cache class, such as RedisStorage class; * * @ param factory: injected via Spring, with parameters configured in application.properties * @ return * / @ Bean public RedisTemplate redisTemplate (RedisConnectionFactory factory) {StringRedisTemplate template = new StringRedisTemplate (factory); setSerializer (template); / / set the serialization tool template.afterPropertiesSet (); return template;} private void setSerializer (StringRedisTemplate template) {Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper om = new ObjectMapper () Om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (om); template.setValueSerializer (jackson2JsonRedisSerializer);} / * * Custom key. * this method will generate a unique key based on the class name + method name + the values of all parameters, even if the value attribute in @ Cacheable is the same, the key will be different. * / @ Override public KeyGenerator keyGenerator () {return new KeyGenerator () {@ Override public Object generate (Object o, Method method, Object...) Objects) {StringBuilder sb = new StringBuilder (); sb.append (o.getClass (). GetName ()); sb.append (method.getName ()); for (Object obj: objects) {sb.append (obj.toString ());} return sb.toString () }};} # REDIS (RedisProperties) basic redis configuration # database namespring.redis.database=0# server host1spring.redis.host=127.0.0.1# server password#spring.redis.password=#connection portspring.redis.port=6379# pool settings... spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool. After reading the above content, max-active=8spring.redis.pool.max-wait=-1# name of Redis server#spring.redis.sentinel.master=# comma-separated list of host:port pairs#spring.redis.sentinel.nodes= Do you have any further understanding of how to configure a single point of Redis cache in SpringBoot? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.