spring Boot 整合Redis

1.配置依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.xuzhang
    spring-boot-redis
    0.0.1-SNAPSHOT
    spring-boot-redis
    Demo project for Spring Boot

    
        1.8
    

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

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.redis configuration

package com.xuzhang.springbootredis.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : objects) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
}

application.properties

# Redis config
spring.redis.database=0
#redis host address
spring.redis.host=localhost
#redis port
spring.redis.port=6379
#redis password, set it empty
spring.redis.password=
#the pool max connection number
spring.redis.lettuce.pool.max-active=8
#
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0

3.编写测试代码

package com.xuzhang.springbootredis;

import com.xuzhang.springbootredis.model.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test() {
        stringRedisTemplate.opsForValue().set("name", "xuzhang");
        Assert.assertEquals("xuzhang", stringRedisTemplate.opsForValue().get("name"));
    }

    @Test
    public void testObj() {
        User user = User.builder().name("xuzhang").password("1234").build();
        ValueOperations operations = redisTemplate.opsForValue();
        operations.set("com.xuzhang", user);
        Assert.assertEquals("xuzhang", operations.get("com.xuzhang").getName());
    }
}

项目参考地址

你可能感兴趣的:(spring Boot 整合Redis)