整合Redis
引入jar包
<!--spring整合redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
入门API测试案例
//@SpringBootTest 如果须要在测试类中引入spring容器机制才应用此注解
public class TestRedis {
/**
* 测试近程redis服务器是否可用
* host: 192.168.126.129
* port: 6379 *思路: 1.实例化对象
* 2.利用对象执行redis命令
* 报错调试:1.查看redis.conf的配置文件是否依照要求批改 ip/爱护/后盾
* 2.redis启动形式 redis-server redis.conf
* 3.敞开防火墙 systemctl stop firewalld.service
*/ @Test
public void test01(){
Jedis jedis=new Jedis("192.168.126.129", 6379);
jedis.set("redis", "测试redis是否可用");
System.out.println(jedis.get("redis"));
}
/**
* String类型API练习
* 需要: 判断key是否存在于redis,如果存在则赋值,否则入库
*/
@Test
public void test02(){
Jedis jedis=new Jedis("192.168.126.129", 6379);
if(jedis.exists("redis")){
System.out.println("数据已存在");
jedis.expire("redis", 10);
}else{
jedis.set("redis","aaaa");
}
System.out.println(jedis.get("redis"));
}
/**
* 能够利用优化的API实现业务性能
* 业务: 如果数据存在则不赋值
*/
@Test
public void test03(){
Jedis jedis=new Jedis("192.168.126.129", 6379);
jedis.flushAll();//清空redis服务器
//jedis.set("redis","111");
//如果key存在则不做任何操作
jedis.setnx("redis", "测试赋值操作!");
System.out.println(jedis.get("redis"));
}
/**
* 测试增加超时工夫的有效性
* 业务: 向redis中保留一个数据之后,要求设定10s无效
* 原子性: 要么同时胜利,要么同时失败
*/
@Test
public void test04(){
Jedis jedis=new Jedis("192.168.126.129", 6379);
/*jedis.set("aa","aa");//数据不删除
int a=1/0; jedis.expire("aa", 10);*/ jedis.setex("aa", 10, "aa");//单位秒
//jedis.psetex()单位毫秒
}
/**
* 需要: 增加一个数据,只有数据存在时才会赋值,并且须要增加超时工夫
* 保障原子性操作
* private static final String XX = "xx";有key才赋值
* private static final String NX = "nx";没有key才赋值
* private static final String PX = "px";毫秒
* private static final String EX = "ex";秒
*
* redis分布式锁问题
*/
@Test
public void test05(){
Jedis jedis=new Jedis("192.168.126.129", 6379);
SetParams setParams=new SetParams();
setParams.xx().ex(10);
jedis.set("aaa", "aaa",setParams);
}
@Test
public void testhash(){
Jedis jedis=new Jedis("192.168.126.129", 6379);
jedis.hset("user", "id", "100");
jedis.hset("user", "name", "tomcat");
System.out.println(jedis.hget("user", "id"));
System.out.println(jedis.hget("user", "name"));
if(jedis.hexists("user", "id")&&jedis.hexists("user", "name")){
System.out.println("存在");
}else {
System.out.println("不存在");
}
System.out.println(jedis.hgetAll("user"));
System.out.println(jedis.hkeys("user"));
jedis.hdel("user", "id","name");
System.out.println(jedis.hgetAll("user"));
}
@Test
public void testlist(){
Jedis jedis=new Jedis("192.168.126.129", 6379);
jedis.lpush("list2", "1,2,3,4,5");
System.out.println(jedis.rpop("list2"));
}
/**
* 管制redis事务
* 阐明:操作单台redis实用于事务管理,然而如果多台redis则不太实用事务
*/
@Test
public void testTx() {
Jedis jedis = new Jedis("192.168.126.129", 6379);
//开启事务
Transaction transaction = jedis.multi();
try {
transaction.set("bb", "bb");
transaction.exec();//提交事务
}catch (Exception e){
transaction.discard();//回滚事务
}
}
}
SpringBoot整合Redis
配置类地位
因为redis之后会被其余的服务器实用,所以最好的形式将Redis的配置类保留到common包下
编辑redis.properties配置文件
编辑JedisConfig配置类
@Configuration//标识我是一个配置类
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
/**
* 将jedis的对象交给spring容器治理
*/
@Bean
public Jedis jedis(){
//因为将代码写死不利于扩大,所以将固定的配置增加到配置文件中
return new Jedis(host,port);
}
}
对象与JSON互相转化
对象转为JSON
@Test
public void test01(){
ObjectMapper objectMapper=new ObjectMapper();
ItemDesc itemDesc=new ItemDesc();
itemDesc.setItemId(100L)
.setItemDesc("json测试")
.setCreated(new Date())
.setUpdated(new Date());
try {
//1.将对象转化为JSON
String result = objectMapper.writeValueAsString(itemDesc);
System.out.println(result);
//2.将JSON转化为对象 只能通过反射机制
//给定xxx.class类型 之后实例化对象,利用对象的get/set办法给属性赋值
ItemDesc itemDesc2
= objectMapper.readValue(result, ItemDesc.class);
System.out.println(itemDesc2);
System.out.println(itemDesc2.getCreated());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
JSON转为对象
@Test
public void test02(){
ObjectMapper objectMapper=new ObjectMapper();
ItemDesc itemDesc=new ItemDesc();
itemDesc.setItemId(100L)
.setItemDesc("json测试")
.setCreated(new Date())
.setUpdated(new Date());
List<ItemDesc> list=new ArrayList<>();
list.add(itemDesc);
list.add(itemDesc);
//1.将对象转化为JSON
try {
String json = objectMapper.writeValueAsString(list);
System.out.println(json);
//2.json转化为对象
List<ItemDesc> list2 = objectMapper.readValue(json, list.getClass());
System.out.println(list2);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
封装ObjectMapperUtil
阐明
为了升高工具API ObjectMapper中的异样解决,咱们能够筹备一些工具API简化代码的调用.
工具API中须要以下内容拍那个:
办法1: 将任意的对象转化为JSON.
办法2: 将任意的JSON串转化为对象.
要求实现异样的解决.
编辑工具API
在common包中增加工具API对象,能够对立应用
public class ObjectMapperUtil {
//定义常量对象
// 劣势1: 对象独一份节俭空间
// 劣势2: 对象不容许他人随便篡改
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 1.将任意对象转化为JSON
* 思考1: 任意对象对象应该应用Object对象来接
* 思考2: 返回值是JSON串 所以应该是String
* 思考3: 应用什么形式转化JSON FASTJSON/objectMapper
*/
public static String toJSON(Object object){
try {
if(object == null){
throw new RuntimeException("传递的参数object为null,请认真查看");
}
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
//应该将查看异样,转化为运行时异样.
throw new RuntimeException("传递的对象不反对json转化/查看是否有get/set办法");
}
}
//2.将任意的JSON串转化为对象 传递什么类型转化什么对象
public static <T> T toObject(String json,Class<T> target){
if(StringUtils.isEmpty(json) || target == null){
throw new RuntimeException("传递的参数不能为null");
}
try {
return MAPPER.readValue(json,target);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException("json转化异样");
}
}
}
测试工具API是否可用
@Test
public void test03(){
ItemDesc itemDesc = new ItemDesc();
itemDesc.setItemId(100L).setItemDesc("json测试")
.setCreated(new Date()).setUpdated(new Date());
String json = ObjectMapperUtil.toJSON(itemDesc);
ItemDesc itemDesc2 =
ObjectMapperUtil.toObject(json, ItemDesc.class);
System.out.println(json);
System.out.println(itemDesc2);
}
实现商品分类的缓存
业务阐明
步骤:
1.查问缓存数据
2.判断缓存中是否咱们所需数据
3.如果没有数据,则查询数据库
4.如果有数据,则间接返回数据
业务实现
@Autowired
private Jedis jedis;
/**
* 步骤:
* 先查问Redis缓存 K:V
* true 间接返回数据
* false 查询数据库
*
* KEY有什么特点: 1.key应该动态变化 2.key应该标识业务属性
* key=ITEM_CAT_PARENTID::parentId
* @param parentId
* @return
*/
@Override
public List<EasyUITree> findItemCache(Long parentId) {
//0.定义空集合
List<EasyUITree> treeList = new ArrayList<>();
String key = "ITEM_CAT_PARENTID::"+parentId;
//1.从缓存中查问数据
String json = jedis.get(key);
//2.校验JSON中是否有值.
if(StringUtils.isEmpty(json)){
//3.如果缓存中没有数据,则查询数据库
treeList = findItemCatList(parentId);
//4.为了实现缓存解决应该将数据增加到redis中.
//将数据转化为json构造,保留到redis中
json = ObjectMapperUtil.toJSON(treeList);
jedis.set(key, json);
System.out.println("第一次查询数据库!!!!");
}else{
//标识程序有值 将json数据转化为对象即可
treeList =
ObjectMapperUtil.toObject(json,treeList.getClass());
System.out.println("查问Redis缓存服务器胜利!!!!");
}
return treeList;
}
发表回复