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);
}
});
}
}