Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the notes on the use of springboot integrated redis

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you what is the content of the comments on the use of springboot integrated redis. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Redis is one of the most popular NOSQL systems. It is an open source key-value storage system written in ANSI C (different from MySQL's two-dimensional table storage). It is similar to Memcache, but largely compensates for Memcache's shortcomings. Like Memcache, Redis data is cached in computer memory. The difference is that Memcache can only cache data into memory and cannot automatically write it to the hard disk regularly. This means that when a power failure or restart occurs, the memory is empty and the data is lost. So Memcache's application scenario is suitable for caching data that doesn't need to be persisted. Redis differs in that it periodically writes updated data to disk or writes modifications to additional log files to achieve data persistence. The characteristics of Redis: 1, Redis read speed is 110000 times/s, write speed is 81000 times/s;2, atomic. All operations in Redis are atomic, and Redis also supports atomic execution of several operations after full union. 3. Support multiple data structures: string (string);list (list);hash (hash); set (set);zset(ordered set)4. Persistence, cluster deployment 5. Support expiration time, support transactions, message subscription introduces dependencies: org.springframework.bootspring-boot-starter-data-redisorg.apache.commonscommons-pool22.6.0 Write the application.properties file #redi Configure spring.redis.host=ip address spring.redis.port= port number spring.redis.database=0spring.redis.password= password spring. reducis.lettuce.pool.max-active=20spring. reducis.lettuce.pool.max-wait=1#maximum blocking wait time (Negative numbers indicate no limit) spring. reduce. let.pool.max-idle=5spring. reduce. let.pool.min-idle=0#shutdown timeout spring. reduce. let.shutdown-timeout=100 Write a configuration class: @EnableCaching@Configurationpublic class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate redisTemplate (RedisConnectionFactoryfactory) {RedisTemplate template = new RedisTemplate();RedisSerializer redisSerializer = new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = newJackson2JsonRedisSerializer (Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setConnectionFactory(factory);//key serialization template.setKeySerializer (redisSerializer);//valueSerialize template.setValueSerializer (jackson2JsonRedisSerializer);//value hashmap serializer template.setHashValueSerializer (jackson2JsonRedisSerializer);return template; }@Beanpublic CacheManager cacheManager (RedisConnectionFactory factory) {RedisSerializer redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = newJackson2JsonRedisSerializer ObjectMapper om = new ObjectMapper();om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);//configure serialization (solve garbled problems), Expiration 600 seconds RedisCacheConfiguration =RedisCacheConfiguration.defaultCacheConfig() .entryTtl (Duration.ofSeconds(600)) .serializeKeysWith (RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith (RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues();RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build();return cacheManager; }} redis annotation introduction of springboot (1) cache @Cacheable

cache the returned result according to the method, if the cache exists, directly read the cached data and return it at the next request; if the cache does not exist, execute the method and store the returned result in the cache. Generally used in query methods.

Check the source code, attribute values are as follows:

(2) Cache @CachePut

Methods that use this annotation tag are executed each time, and the results are stored in the specified cache. Other methods can read cached data directly from the cache of responses without having to query the database. Usually used in new methods.

View source code, attribute values are as follows

(3) Cache @CacheEvict

The method using this annotation flag empties the specified cache. Generally used in updating or deleting methods to view source code, attribute values are as follows

Unable to connect redis: (1) Turn off liunx firewall (2) Find redis configuration file: modify protected-mode yes to protected-mode no comment out: bind 127.0.0.1 test @Cacheable (value = "banner", key = "'selectIndexList'")@Overridepublic List selectIndexList() {List list = baseMapper.selectList (newQueryWrapper().orderByDesc("sort"));return list; }@CacheEvict(value = "banner", allEntries=true)@Overridepublic void removeBannerById(String id) {baseMapper.deleteById(id); }

In redis:

Basic functions:

Query using: @Cacheable annotation

Modify delete: use @CacheEvict annotation

Added: Use @CachePut annotation

Thank you for reading! About "springboot integrated redis use comments what" this article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report