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

How to use centralized caching Redis in Spring Boot 2.x

2025-01-18 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 use centralized cache Redis in Spring Boot 2.x, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

Although EhCache can be applied to many application scenarios, because EhCache is an intra-process caching framework, in cluster mode, the caches between application servers are independent, so there will be cache inconsistencies between processes on different servers. Even though EhCache provides cache synchronization strategy in cluster environment, synchronization still takes some time, and temporary cache inconsistencies still exist.

In some systems and applications that require high consistency (any data changes can be queried in time), EhCache can no longer be used to solve the problem. At this time, centralized caching can be used to solve the problem of cache data consistency. Next, let's learn how to use Redis to implement data caching in Spring Boot's cache support.

Give it a try.

The implementation of this article will be based on the basic project of the previous article. Let's review the procedural elements in the previous article:

Definition of User entity

@ Entity@Data@NoArgsConstructorpublic class User implements Serializable {@ Id @ GeneratedValue private Long id; private String name; private Integer age; public User (String name, Integer age) {this.name = name; this.age = age;}}

Data access implementation of User entities (covers cache annotations)

@ CacheConfig (cacheNames = "users") public interface UserRepository extends JpaRepository {@ Cacheable User findByName (String name);}

Let's start revamping the project:

Step 1: add related dependencies to pom.xml:

Org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2

In earlier versions of Spring Boot 1.x, the dependency was named spring-boot-starter-redis, so it was different in the basic Spring Boot 1.x tutorial.

Step 2: add configuration information to the configuration file, taking local operation as an example, such as:

Spring.redis.host=localhostspring.redis.port=6379spring.redis.lettuce.pool.max-idle=8spring.redis.lettuce.pool.max-active=8spring.redis.lettuce.pool.max-wait=-1msspring.redis.lettuce.pool.min-idle=0spring.redis.lettuce.shutdown-timeout=100ms

Note a few points about the configuration of connection pooling:

Redis's connection pooling configuration is prefixed as spring.redis.pool in version 1.x, which is different from Spring Boot 2.x.

Jedis is used as the connection pool in version 1.x and lettuce as the connection pool in version 2.x

The above configurations are all default values. In fact, production needs to be further modified according to deployment and business requirements.

Let's try unit testing again:

@ Slf4j@RunWith (SpringRunner.class) @ SpringBootTestpublic class Chapter54ApplicationTests {@ Autowired private UserRepository userRepository; @ Autowired private CacheManager cacheManager; @ Testpublic void test () throws Exception {System.out.println ("CacheManager type:" + cacheManager.getClass ()); / / create a record userRepository.save (new User ("AAA", 10); User U1 = userRepository.findByName ("AAA") System.out.println ("first query:" + u1.getAge ()); User U2 = userRepository.findByName ("AAA"); System.out.println ("second query:" + u2.getAge ());}}

By executing the test output, you can get:

CacheManager type: class org.springframework.data.redis.cache.RedisCacheManagerHibernate: select next_val as id_val from hibernate_sequence for updateHibernate: update hibernate_sequence set next_val=? Where next_val=?Hibernate: insert into user (age, name, id) values (?,?) 2020-08-12 16 where next_val=?Hibernate 25 name 26.954 INFO 68282-[main] io.lettuce.core.EpollProvider: Starting without optional epoll library2020-08-12 16 16 age 25 Fringe 26.955 INFO 68282-[main] io.lettuce.core.KqueueProvider: Starting without optional kqueue libraryHibernate: select user0_.id as id1_0_ User0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=? First query: 10 second query: 10

You can see:

The CacheManager type output on the first line is org.springframework.data.redis.cache.RedisCacheManager instead of EhCacheCacheManager in the previous article.

In the second query, no SQL statement was output, so it was cached and fetched

The integration is successful!

After reading the above, do you have any further understanding of how to use centralized caching Redis in Spring Boot 2.x? 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.

Share To

Internet Technology

Wechat

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

12
Report