SpringBoot-redis
引依赖
xml<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
添加Redis连接信息配置
server:
servlet:
context-path: /springboot-redis
port: 8081
logging:
level:
org.redis: debug
spring:
redis:
database: 0
host: 192.168.XX.XX
port: 6379
password: xxxx
timeout: 3000
jedis:
pool:
max-active: 200
max-wait: -1
max-idle: 10
min-idle: 0
RedisTemplate 序列化相关配置:
package cn.xiaocai.demo.redis.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @program: bp-paystation-client
* @description
* @function: redis 的相关配置
* @author: zzy
* @create: 2021-04-29 15:08
**/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(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);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
好了,如果连接没问题基本就可以正常启动了。
使用的话 RedisTemplate 操作API就可以了。如果觉得比较麻烦,可以封装一个RedisUtil
工具类或者service,网上搜一下,不少。
测试使用示例:
package cn.xiaocai.demo.redis.web;
import cn.xiaocai.demo.redis.common.RedisUtil;
import cn.xiaocai.demo.redis.po.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @program: spring-boot-redis
* @description
* @function:
* @author: zzy
* @create: 2021-04-29 16:47
**/
@RestController
@Slf4j
@AllArgsConstructor
@Api(tags = "Redis-ops 测试")
public class RedisController {
private final RedisUtil redisUtil;
@ApiOperation("redis")
@ApiImplicitParams({
@ApiImplicitParam(name = "key", value = "缓存key", defaultValue = "key", required = true),
@ApiImplicitParam(name = "value", value = "缓存value", defaultValue = "value", required = true)
})
@GetMapping("redis/{key}/{value}")
public String redisOps(@PathVariable("key") String key, @PathVariable("value") String value){
boolean b1 = redisUtil.set("key", "value");
boolean b2 = redisUtil.set("1", "test1111");
String cai = (String) redisUtil.get("zzy");
log.info(" b1 = " +b1+" b2 = " +b2+" cai = "+cai);
redisUtil.expire("1", 10, TimeUnit.SECONDS);
boolean bk = redisUtil.set(key, value);
redisUtil.expire(key, 15, TimeUnit.SECONDS);
log.info(" bk = " +bk);
return "SUCCESS";
}
private static HashSet set = new HashSet<User>();
private static List roles = new ArrayList();
private static HashMap map = new HashMap<String,User>();
static {
User user1 = new User(9733L, "A1", new Date(),LocalDateTime.now(), true, "what is your name ".getBytes());
User user2 = new User(9734L, "A2", new Date(),LocalDateTime.now(), false, "what is your name ".getBytes());
User user3 = new User(9735L, "A3", new Date(),LocalDateTime.now(), true, "what is your name ".getBytes());
User user4 = new User(9736L, "A4", new Date(),LocalDateTime.now(), false, "what is your name ".getBytes());
User user5 = new User(9737L, "A5", new Date(),LocalDateTime.now(), true, "what is your name ".getBytes());
set.add(user1);
set.add(user2);
map.put(user4.getId(), user1);
map.put(user5.getId(), user1);
roles.add(user1);
roles.add(user3);
roles.add(user5);
}
@ApiOperation("redis-obj")
@PostMapping("redis-obj")
public String redisObj(){
User user = new User();
user.setId(9733L);
user.setImage("new FileInputStream(new File())".getBytes());
user.setInDate(new Date());
user.setOutTime(LocalDateTime.now());
user.setOk(true);
user.setLeaders(set);
user.setRoles(roles);
user.setPress(map);
redisUtil.set("user:"+user.getId(), user.toString());
redisUtil.expire("user:"+user.getId(), 60, TimeUnit.SECONDS);
return "SUCCESS";
}
}
问题1:工具类中
@Autowired
private RedisTemplate<String, Object> redisTemplate;
无法注入,把类型去掉就好了。
问题2:存储到redis的时候,key和value出现了\xac\xed\x00\x05t\x00\