Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use centralized cache Redis in Spring Boot

Shulou Source: shulou.com Published: 2022-06-01 12:20:49 11月01日 Update

This article introduces the knowledge of "how to use centralized cache Redis in Spring Boot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Try the definition of User entity

@ Entity @ Data @ NoArgsConstructor public 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);}

(recommended course: Spring tutorial)

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=localhost spring.redis.port=6379 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1ms spring.redis.lettuce.pool.min-idle=0 spring.redis.lettuce.shutdown-timeout=100ms

With regard to the configuration of connection pooling, you need to note:

Redis's connection pooling configuration is prefixed as spring.redis.pool in version 1.x, which is different from Spring Boot 2.x. In version 1.x, jedis is used as the connection pool, while in version 2.x, lettuce is used as the connection pool. All the above configurations are 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) @ SpringBootTest public class Chapter54ApplicationTests {

@ Autowired private UserRepository userRepository; @ Autowired private CacheManager cacheManager; @ Test public 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.RedisCacheManager Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val=? Where nextval=? Hibernate: insert into user (age, name, id) values (?,?) 2020-08-12 16 Hibernate 25 name 26.954 INFO 68282-[main] io.lettuce.core.EpollProvider: Starting without optional epoll library 2020-08-12 16 V V 25 Vane 26.955 INFO 68282-[main] io.lettuce.core.KqueueProvider: Starting without optional kqueue library Hibernate: select user0.id as id10, user0_.age as age20, user0_.name as name30 from user user0 where user0.name=? First query: 10 second query: 10

(recommended micro-class: Spring micro-class)

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

This is the end of "how to use centralized caching Redis in Spring Boot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Query configuration cache version output different content entity actual situation tutorial more knowledge first time recommendation test practical appropriate learned next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Shulou Tech Info Xiaomi Docker