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 integrate Redis cache with SpringBoot

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

How does SpringBoot integrate Redis cache? In response to this problem, the editor summed up this article today, hoping to help more friends who want to solve this problem to find a more simple and feasible way.

1. Introduce cache dependency

Org.springframework.boot spring-boot-starter-data-redis 2.1.5.RELEASE2. Increase cache configuration

Add the following configuration to the application.properties file

# # Redis part # Redis server address spring.redis.host=$ {redis.host} # Redis server connection port spring.redis.port=$ {redis.port} # Redis server connection password (default is empty) spring.redis.password=$ {redis.password} # maximum number of connections in connection pool (use negative values for no limit) spring.redis.jedis.pool.max-active=$ {redis.maxTotal} # maximum blocking wait time in connection pool (using Negative value indicates no limit) maximum idle connection in spring.redis.jedis.pool.max-wait=-1ms# connection pool spring.redis.jedis.pool.max-idle=$ {redis.maxIdle} # minimum idle connection spring.redis.jedis.pool.min-idle=4# connection timeout in milliseconds) spring.redis.timeout=5000## Cache part # cached name collection There are many types of spring.cache.cache-names=# cache separated by commas, which are officially provided. Here we fill in whether redisspring.cache.type=redis# caches null data. The default is the cache timeout in falsespring.cache.redis.cache-null-values=false#redis. The default is whether the 60000msspring.cache.redis.time-to-live=60000# cache data key uses a prefix, and the default is the prefix of truespring.cache.redis.use-key-prefix=true# cache data key. Valid when the above configuration is true, spring.cache.redis.key-prefix=3. Added to enable cache annotation EnableCaching

@ EnableCachingpublic class WebApplication {public static void main (String [] args) {SpringApplication.run (WebApplication.class, args);}} 4. Add cache comments

@ Cacheable

The annotation is used to identify that the return value of this method will be cached

Note that condition and unless are both conditional parameters:

Condition: the judgment is made before the method is called, so the result value of the method cannot be used as a judgment condition

Unless: the judgment is made after the method is called. At this time, the method playback value can be taken as the judgment condition.

So depending on the method return value as a caching operation must use the unless parameter instead of condition

@ CachePut

Update the current cache with the method return value

@ CacheEvict

Expire the current cache (empty)

This is the end of SpringBoot's method of integrating Redis cache. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Database

Wechat

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

12
Report