cgb2007-京淘day13

1.redis其他数据类型2.SpringBoot整合jedis对象3.对象与JSON转化的方法4.缓存实现商品分类查询5.AOP优化redis缓存
摘要由CSDN通过智能技术生成

1.Redis命令

1.1 入门案例操作

 @Test
    public void testHash() throws InterruptedException {
   
        Jedis jedis = new Jedis("192.168.126.129",6379);
        jedis.hset("person", "id", "18");
        jedis.hset("person", "name", "hash测试");
        jedis.hset("person", "age", "2");
        Map<String,String> map = jedis.hgetAll("person");
        Set<String> set = jedis.hkeys("person");    //获取所有的key
        List<String> list = jedis.hvals("person");
    }

    @Test
    public void testList() throws InterruptedException {
   
        Jedis jedis = new Jedis("192.168.126.129",6379);
        jedis.lpush("list", "1","2","3","4");
        System.out.println(jedis.rpop("list"));
    }

    @Test
    public void testTx(){
   
        Jedis jedis = new Jedis("192.168.126.129",6379);
        //1.开启事务
       Transaction transaction =  jedis.multi();
       try {
   
           transaction.set("a", "a");
           transaction.set("b", "b");
           transaction.set("c", "c");
           transaction.exec();  //提交事务
       }catch (Exception e){
   
           transaction.discard();
       }
    }

2 SpringBoot整合Redis

2.1 编辑pro配置文件

由于redis的IP地址和端口都是动态变化的,所以通过配置文件标识数据. 由于redis是公共部分,所以写到common中.
在这里插入图片描述

2.2 编辑配置类

package com.jt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.Jedis;

@Configuration  //标识我是配置类
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
   

    @Value("${redis.host}")
    private String  host;
    @Value("${redis.port}")
    private Integer port;

    @Bean
    public Jedis jedis(){
   

        return new Jedis(host, port);
    }
}

3 对象与JSON串转化

3.1 对象转化JSON入门案例

package com.jt;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.pojo.ItemDesc;
import org.junit.jupiter.api.Test;

  • 17
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闪耀太阳

感觉文章不错的记得打赏呀

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值