添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
添加配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制) 默认为8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认为-1
spring.redis.lettuce.pool.max-wait=-1ms
# 连接池中的最大空闲连接 默认为8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认为 0
spring.redis.lettuce.pool.min-idle=0
自定义 RedisTemplate
package com.example.demo.redis;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
public class ObjectRedisTemplate extends RedisTemplate<String, Object> {
public ObjectRedisTemplate() {
setKeySerializer(RedisSerializer.string());
setValueSerializer(new GenericFastJsonRedisSerializer());
setHashKeySerializer(RedisSerializer.string());
setHashValueSerializer(new GenericFastJsonRedisSerializer());
}
public ObjectRedisTemplate(RedisConnectionFactory connectionFactory) {
this();
setConnectionFactory(connectionFactory);
afterPropertiesSet();
}
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
return new DefaultStringRedisConnection(connection);
}
}
package com.example.demo.redis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@Configuration
public class RedisConfig {
@Bean
public ObjectRedisTemplate objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
return new ObjectRedisTemplate(redisConnectionFactory);
}
}
定义测试实体类
package com.example.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
}
定义测试类
package com.example.demo;
import com.example.demo.entity.User;
import com.example.demo.redis.ObjectRedisTemplate;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
@SpringBootTest
@Slf4j
public class RedisTest {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private ObjectRedisTemplate objectRedisTemplate;
@Test
public void testObject() {
String key = "user:1";
objectRedisTemplate.opsForValue().set(key, new User(1, "郭碧婷", 20));
User user = (User) objectRedisTemplate.opsForValue().get(key);
log.info("uesr: " + user);
}
@Test
public void testString() {
stringRedisTemplate.opsForValue().set("key", "唐嫣");
String key = stringRedisTemplate.opsForValue().get("key");
log.info(key);
}
@Test
public void testTime() {
stringRedisTemplate.opsForValue().set("key", "鱼闪闪", 20, TimeUnit.SECONDS);
String key = stringRedisTemplate.opsForValue().get("key");
log.info(key);
}
@Test
public void testTime2() {
stringRedisTemplate.opsForValue().set("key", "鱼闪闪", Duration.ofSeconds(20));
String key = stringRedisTemplate.opsForValue().get("key");
log.info(key);
}
}
源码地址
https://github.com/ofoo/my-spring-boot/tree/master/spring-boot-redis
|