1、主要介绍两张不同的使用方法:
package com.haoxiansheng.middleware.springMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.haoxiansheng.middleware.MainApplicationTest;
import com.haoxiansheng.middleware.rabbitmq.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.Optional;
@Slf4j
public class RedisTest extends MainApplicationTest {
@Autowired
private RedisTemplate redisTemplate; //配置中自定义注入 在直接自动装配
@Autowired
private ObjectMapper objectMapper; // 定义JSON 序列化与反序列化框架类
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 采用RedisTemplate 将字符串信息写入缓存中并读取出来
*/
@Test
public void tetRedis() {
log.info("=======开始RedisTemplate 操作组件实战");
//定义字符串内容及存入缓存的Key
final String content = "RedisTemplate实战字符串信息";
final String key = "redis:template:one:string";
ValueOperations valueOperations = redisTemplate.opsForValue(); //Redis 通用操作组件
log.info("写入缓冲中的内容:{}", content);
valueOperations.set(key, content); //将信息写入缓冲中
Object result = valueOperations.get(key); //从缓冲中读取内容
log.info("读取出来的内容:{}", result);
}
/**
* 采用RedisTemplate 将对象信息序列化为JSON 格式字符串后写入缓冲中
* 然后将其读取出来, 最后反序列化解析其中的内容并展示在控制台上
*/
@Test
public void testRedisJSON() {
log.info("==开始RedisTemplate组件实战操作==");
User user = new User(1, "debug", "阿修罗");
ValueOperations valueOperations = redisTemplate.opsForValue(); //Redis 通用操作组件
final String key = "redis:template:one:object";
/**
* 将序列化后的信息写入缓冲中
*/
try {
final String content = objectMapper.writeValueAsString(user);
valueOperations.set(key, content);
log.info("写入缓冲对象的信息:{}", user);
} catch (Exception e) {
log.error("json 序列化失败:{}", user);
}
/**
* 从缓冲中读取内容
*/
Optional.ofNullable(valueOperations.get(key)).ifPresent(x -> {
try {
User userReceive = objectMapper.readValue(x.toString(), User.class);
log.info("读取内容并反序列化后的结果:{}", userReceive);
} catch (Exception e) {
log.error("读取信息异常,{}", x);
}
});
}
/**
* 下面是采用StringRedisTemplate 实现上面两个需求
*/
@Test
public void testRedisString() {
log.info("======开始StringRedisTemplate 操作组件实战======");
final String content = "StringRedisTemplate 实战字符串信息";
final String key = "redis:String ==>";
log.info("写入缓冲的内容:{}", content);
ValueOperations valueOperations = stringRedisTemplate.opsForValue();
valueOperations.set(key, content);
Object result = valueOperations.get(key);
log.info("读取出来的内容:{}", result);
}
@Test
public void testRedisStringObject() {
log.info(" ===开始StringRedisTemplate操作组件实战===");
User user = new User(2, "JSON==Oject", "阿凡达");
ValueOperations valueOperations = stringRedisTemplate.opsForValue();
final String key = "redis:string:object ==>";
try {
final String content = objectMapper.writeValueAsString(user);
valueOperations.set(key, content);
log.info("写入缓冲对象的信息:{}", content);
} catch (Exception e) {
log.error("JSON 序列化失败:{}", user);
}
Optional.ofNullable(valueOperations.get(key)).ifPresent(x -> {
try {
User userReceive = objectMapper.readValue(x.toString(), User.class);
log.info("读取信息并返回序列化的结果:{}", userReceive);
} catch (Exception e) {
log.error("读取信息异常: {}", x);
}
});
}
}