SpringBoot整合Redis

编辑配置文件 redis.proper

说明:由于该配置被其他的项目共同使用,则应该写到common中。
SpringBoot整合Redis_第1张图片

编辑配置类

说明:编辑redis配置类,将Jedis对象交给Spring容器进行管理。
SpringBoot整合Redis_第2张图片

@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {
    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;
    @Bean
 public Jedis jedis(){
        return new Jedis(host,port);
    }
}

对象与JSON转化(ObjectMapper介绍)

简单对象转化

/**
     * 测试简单对象的转化
     */
    @Test
    public void test01() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(100L).setItemDesc("商品详情信息")
                .setCreated(new Date()).setUpdated(new Date());
        //对象转化为json
        String json = objectMapper.writeValueAsString(itemDesc);
        System.out.println(json);

        //json转化为对象
        ItemDesc itemDesc2 = objectMapper.readValue(json, ItemDesc.class);
        System.out.println(itemDesc2.getItemDesc());
    }

集合对象转化

 //测试集合对象的转化
 @Test
 public void testJson01() throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
        ItemDesc itemDesc1=new ItemDesc();
        itemDesc1.setItemId(100L).setItemDesc("商品信息详情1")
                .setCreated(new Date()).setUpdated(new Date());
        ItemDesc itemDesc2=new ItemDesc();
        itemDesc2.setItemId(100L).setItemDesc("商品信息详情2")
                .setCreated(new Date()).setUpdated(new Date());
        List list=new ArrayList<>();
        list.add(itemDesc1);
        list.add(itemDesc2);
        //将集合对象转化为json格式
 String json = objectMapper.writeValueAsString(list);
        System.out.println(json);//[{"created":1604828831646,"updated":1604828831646,"itemId":100,"itemDesc":"商品信息详情1"},{"created":1604828831646,"updated":1604828831646,"itemId":100,"itemDesc":"商品信息详情2"}]
 //将json格式串转化为集合对象
 List list1 = objectMapper.readValue(json, list.getClass());
        System.out.println(list1);//[{created=1604828831646, updated=1604828831646, itemId=100, itemDesc=商品信息详情1}, {created=1604828831646, updated=1604828831646, itemId=100, itemDesc=商品信息详情2}]
 }
}

编辑工具API

SpringBoot整合Redis_第3张图片

public class ObjectMapperUtil {
    /**
 * 1.将用户传递的数据转化为json
 * 2.将用户传递的json转化为对象
 */
 private static final ObjectMapper MAPPER=new ObjectMapper();
    //1.将用户传递的数据转化为json
 public static String toJson(Object object){
        if(object==null){
            throw new RuntimeException("传递的数据不能为空,请检查");
        }
        try {
           return MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            //将检查异常转化为运行时异常
 e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    //2.将用户传递的json转化为对象
 //需求:要求用户传递什么样的类型,就返回什么样的对象(运用泛型的知识)
 public static  T toObj(String json,Class target){
        if(StringUtils.isEmpty(json)||target==null)
            throw new RuntimeException("参数不能为空");
        try {
            return MAPPER.readValue(json, target);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

商品分类的缓存实现

实现步骤

1.定义Redis中的key。key必须唯一不能重复,设计到存和取。【格式应该是:key="ITEM_CAT_PARENTID::70"】
2.根据key去redis中进行查询数据(有数据|没有数据)。
3.没有数据则查询数据库获取巨鹿并将查询出来的数据保存到redis中,方便后续使用。
4.有数据表示用户不是第一次查询,可以将缓存数据直接返回即可。

编辑ItemCatController

SpringBoot整合Redis_第4张图片

编辑ItemCatService

/**
 * 1.定义Redis中的key,
 * 这里的key要求唯一还不能重复,涉及到存和取
 *      key="ITEM_CAT_PARENTID::"+parentId;
 * 2.根据key去redis中进行查询   要么有数据 要么没有数据
 * 3.如果没有数据则查询数据库获取记录,之后将数据保存在redis中,方便后续使用
 * 4.如果有数据则表示用户不是第一次查询,可以将缓存数据也直接返回即可
 *
 */@Override
public List findItemCatListCache(Long parentId) {
    //定义一个公共的返回值对象
 List treeList=new ArrayList<>();
    //1.定义Redis中的key,
 String key="ITEM_CAT_PARENTID::"+parentId;
   //测试redis中的执行时间和查询数据库的时间
 Long startTime= System.currentTimeMillis();
    //检索redis中的key是否存在
 if(jedis.exists(key)){
        //数据存在
 String json=jedis.get(key);
        //需要将json串转化为对象,并将转化后的对象存入treeList
 treeList= ObjectMapperUtil.toObj(json, treeList.getClass());
        Long endTime= System.currentTimeMillis();
        System.out.println("redis执行时间"+(endTime-startTime));
    }else{
        //数据不存在 则调用上面的查询方法在数据库中查询数据
 treeList= findItemCatList(parentId);
        //将数据保存到缓存中,以便后续查询方便
 String json = ObjectMapperUtil.toJson(treeList);
        jedis.set(key,json);
        Long endTime= System.currentTimeMillis();
        System.out.println("数据库查询执行时间"+(endTime-startTime));
    }
    return treeList;
}

使用redis的速度差

SpringBoot整合Redis_第5张图片

你可能感兴趣的:(java,redis)