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 is the method of integrating redis with springboot

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the method of springboot integrating redis". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of springboot integrating redis".

Overview

Springboot usually integrates redis in the form of RedisTemplate. In addition to this form, there is another form of integration, that is, using annotations supported by spring to access caching.

"preparation work

Pom.xml file:

Redis.clients

Jedis

2.7.3

Org.springframework.data

Spring-data-redis

1.7.2.RELEASE

Org.springframework.boot

Spring-boot-starter-redis

RELEASE

Application.properties profile:

# REDIS (RedisProperties)

# Redis database index (default is 0)

Spring.redis.database=0

# Redis server address

Spring.redis.host=127.0.0.1

# Redis server connection port

Spring.redis.port=6379

# maximum number of connections in connection pool (use negative values to indicate no limit)

Spring.redis.pool.max-active=8

# maximum blocking wait time for connection pooling (use negative values to indicate no limit)

Spring.redis.pool.max-wait=-1

# maximum idle connection in the connection pool

Spring.redis.pool.max-idle=8

# minimum idle connections in the connection pool

Spring.redis.pool.min-idle=0

# connection timeout (milliseconds)

Spring.redis.timeout=0

Redis configuration class

/ * *

* @ author hulonghai

* redis configuration class

, /

@ Configuration

@ EnableCaching

Public class CacheConfig extends CachingConfigurerSupport {

@ SuppressWarnings ("rawtypes")

@ Bean

Public CacheManager cacheManager (RedisTemplate redisTemplate) {

RedisCacheManager rcm = new RedisCacheManager (redisTemplate)

/ / multiple cache names. Only one is defined.

Rcm.setCacheNames (Arrays.asList ("thisredis"))

/ / set cache expiration time (seconds)

Rcm.setDefaultExpiration (600)

Return rcm

}

@ Bean

Public RedisTemplate redisTemplate (RedisConnectionFactory factory) {

StringRedisTemplate template = new StringRedisTemplate (factory)

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)

Template.afterPropertiesSet ()

Return template

}

}

As you can see, we mainly configure two things here. The cacheManager method is configured with a cache name, which is called thisredis. When we want to use it in the method annotations, we have to distinguish different caches according to the name. At the same time, buffer is set.

The expiration time of the deposit. RedisTemplate is more common, we set RedisTemplate, so in the code, we can also use @ Autowired to inject RedisTemplate to manipulate redis.

Use

The next step is how to use annotations, which is the easiest step. In fact, only two annotations are used, @ Cacheable and @ CacheEvict. The first annotation represents querying the specified key from the cache and, if any, fetching it from the cache and no longer executing the method. If not, hold on.

Line method and associate the return value of the method with the specified key and put it in the cache. @ CacheEvict clears the data corresponding to the specified key from the cache. The code used is as follows:

@ Cacheable (value= "thisredis", key= "'users_'+#id")

Public User findUser (Integer id) {

User user = new User ()

User.setUsername ("hlhdidi")

User.setPassword ("123")

User.setUid (id.longValue ())

System.out.println ("log4j2 is broken?")

Logger.info ("enter user, username: {}, password: {}", user.getUsername (), user.getPassword ())

Return user

}

@ CacheEvict (value= "thisredis", key= "'users_'+#id", condition= "# identices1")

Public void delUser (Integer id) {

/ / Delete user

System.out.println ("user delete")

}

As you can see, we specify the specific cache with the value attribute of @ Cacheable and put it in the cache through key. Here key is very flexible and supports the el expression of spring. You can generate a variable key through method parameters (see findUser method), or you can specify it in the

Under what circumstances, use / do not use caching (see delUser method).

Thank you for your reading, the above is the content of "what is the method of springboot integrating redis". After the study of this article, I believe you have a deeper understanding of what the method of springboot integration of redis is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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