redis实现商品分类查询

该文讲述了如何使用SpringBoot管理Redis服务,通过Jedis实现缓存策略,包括动态获取主机信息,利用Redis的String类型存储商品分类数据,以及使用ObjectMapper工具进行对象与JSON的转换。当数据首次查询时从数据库获取并存入Redis,后续查询直接从缓存读取。

缓存实现策略

1.  redis服务交给SpringBoot进行管理.最好动态的获取主机IP和端口号.

2.  利用RedisString类型实现商品分类缓存.

        Key: 包名.类名.方法名::父级ID

        Value: List<EasyUITree>java对象--JSON----  字符串Redis

3.  第一次查询时,查询数据库信息,之后将查询的结果保存到redis.不用设定超时时间.

     当用户第二次查询时,应该直接获取缓存数据信息.之后给用户返回.

SpringBoot管理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>

编辑redis.properties文件

resource目录中创建properties/redis.propreties

#配置redis主机信息
redis.node=192.168.126.174:6379

编辑配置类

配置类相当于早期的配置文件.将对象交给spring容器管理.最好将redis的配置文件信息存入common,以后其他的业务服务器也可以正常使用.

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

    @Value("${redis.node}")
    private String redisNode;//IP:PORT

    @Bean//标识实例化对象的类型
    @Scope("prototype")//对象的多例
    public Jedis jedis(){
        String host = redisNode.split(":")[0];
        int port = Integer.parseInt(redisNode.split(":")[1]);
        return new Jedis(host,port);
    }
}

Mapper工具API封装

简化程序的调用,并且简化代码的结构

public class ObjectMapperUtil {
    //1.定义Mapper对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    //2.对象转化为JSON
    public static String toJson(Object obj) {
        //异常转化规则:将检查异常转化为运行时异常
        String result = null;
        try {
            result = MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return result;
    }

    //3.JSON串转化为对象
    //需求:用户传递了什么类型,就需要返回什么对象.
    //自定义<T>
    public static <T> T toObject(String json, Class<T> targetClass) {
        T t = null;
        try {
            t = MAPPER.readValue(json, targetClass);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return t;
    }
}

实现商品分类缓存

    /**
     * 1.需要注入jedis对象
     * 2.定义key包名.类名.方法名::第一个参数
     * 3.一开始先查询缓存,如果缓存中有数据则执行缓存业务.
     * 如果缓存中没有数据则查询数据库.
     */
    @Autowired
    private ItemCatMapper itemCatMapper;
    @Autowired
    private Jedis jedis;
    @Override
    public List<EasyUITree> findListByCache(Long parentId) {
        String key = "com.jt.service.ItemCatServiceImpl.findListByCache::"+parentId;
        String value = jedis.get(key);//根据key,查询值
        List<EasyUITree>easyUITreeList = new ArrayList<>();
        if(StringUtils.isEmpty(value)){
            //缓存中没有数据,查询数据库.需要将数据存入redis
            easyUITreeList = findListByParentId(parentId);
            String json = ObjectMapperUtil.toJson(easyUITreeList);
            jedis.set(key,json);//将数据保存到redis中.
        }else {
            //缓存中有数据,需要将JSON转化为对象
            easyUITreeList = ObjectMapperUtil.toObject(value,easyUITreeList.getClass());
        }
        return easyUITreeList;
    }

    @Override
    public List<EasyUITree> findListByParentId(Long parentId) {
        QueryWrapper<ItemCat>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<ItemCat>itemCatList=itemCatMapper.selectList(queryWrapper);
        List<EasyUITree>easyUITreeList = new ArrayList<>(itemCatList.size());
        for (ItemCat itemCat: itemCatList) {
            Long id = itemCat.getId();
            String text = itemCat.getName();
            String state = itemCat.getIsParent()?"closed":"open";
            EasyUITree easyUITree = new EasyUITree(id,text,state);
            easyUITreeList.add(easyUITree);
        }
        return easyUITreeList;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值