Home >Database >Redis >How to use redis in spring

How to use redis in spring

藏色散人
藏色散人Original
2019-06-26 11:44:062488browse

How to use redis in spring

How to use redis in spring?

Using Redis in Spring

Jedis is used to operate Redis in Java. First, add relevant dependencies in pom.xml:

<!-- redis cache related.....start -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>
<!-- redis cache related.....end -->

Then implement the configuration class:

package com.ehelp.util;
 
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
 
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
 
@Bean
public JedisConnectionFactory redisConnectionactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName("localhost");
redisConnectionFactory.setPort(6379);
return redisConnectionFactory;
}
 
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManger = new RedisCacheManager(redisTemplate);
cacheManger.setDefaultExpiration(5); //cache过期时间
return cacheManger;
}
}

Note:

Set the Cache expiration time appropriately. If it is too long, it will be effective for a long time. If it is too short, you will not see the test results. Recommended 5-20 seconds.

Finally, you can use annotations directly on the method that needs to be cached to implement caching:

How to use redis in spring

For more Redis related knowledge, please visit Redis usage Tutorial column!

The above is the detailed content of How to use redis in spring. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Why use redis?Next article:Why use redis?