黑马点评——添加商户缓存、商铺列表缓存

商铺缓存

java 复制代码
    /**
     * 根据id查询商铺信息
     * @param id 商铺id
     * @return 商铺详情数据
     */
    @GetMapping("/{id}")
    public Result queryShopById(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }
java 复制代码
package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.Shop;
import com.hmdp.mapper.ShopMapper;
import com.hmdp.service.IShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.hmdp.utils.RedisConstants.CACHE_SHOP_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryById(Long id) {
        //1.从redis查商铺缓存
        String key =CACHE_SHOP_KEY + id;
        String shopJson = stringRedisTemplate.opsForValue().get(key);

        //2.判断是否存在 如果不为空(存在)
        if(StrUtil.isNotBlank(shopJson)){
            //3.存在,直接返回
            //反序列化json
            Shop shop = JSONUtil.toBean(shopJson, Shop.class);
            return Result.ok(shop);
        }
        //4.不存在,根据id查数据库
        Shop shop = getById(id);
        //5.数据库不存在,返回错误
        if(shop == null){
            return Result.fail("店铺不存在");
        }
        //6.存在,写入redis
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop));

        //7.返回
        return Result.ok(shop);
    }
}
    

商铺列表缓存

java 复制代码
    @GetMapping("list")
    public Result queryTypeList() {
//        List<ShopType> typeList = typeService
//                .query().orderByAsc("sort").list();
//        return Result.ok(typeList);
        return typeService.queryByList();

    }
java 复制代码
package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryByList() {
        //查询缓存是否存在
        String key = "cache:shop:type";
        String shopTypeJson = stringRedisTemplate.opsForValue().get(key);

        //存在返回
        if(StrUtil.isNotBlank(shopTypeJson)){
            List<ShopType> shopTypeList = JSONUtil.toList(shopTypeJson, ShopType.class);
            return Result.ok(shopTypeList);
        }

        //不存在 查询数据库
        List<ShopType> shopTypeList = query().orderByAsc("sort").list();

        //不存在返回错误
        if(shopTypeList == null){
            return Result.fail("店铺类型不存在");
        }

        //添加缓存
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shopTypeList));

        //返回
        return Result.ok(shopTypeList);
    }
}
相关推荐
XLYcmy19 小时前
核内调度问题的分层优化:缓存管理与性能均衡策略 模型评价 模型缺点与改进方向
缓存·ai·启发式算法·图神经网络·遗传算法·数模·模拟退火
无小道21 小时前
Redis——集群
redis·集群
用户6757049885021 天前
Redis 分布式锁的 5个坑,真的是又大又深!!
redis·后端
spider_xcxc1 天前
Redis 深度实践:安全管控、性能压测与持久化分析(二)
运维·前端·redis·云计算·bootstrap·运维开发
随风一样自由1 天前
【前端+React+缓存竞态】导航切换缓存刷新机制
前端·react.js·缓存
桌面运维家1 天前
如何用半缓存云桌面将服务器硬盘容量扩展至本地终端?
运维·服务器·缓存
spider_xcxc2 天前
Redis 数据库高质量实践指南(一)
运维·数据库·redis·oracle·云计算
YOU OU2 天前
Redis初识
数据库·redis·缓存
livemetee2 天前
【关于redis高性能,高可用处理】
数据库·redis·缓存
Omics Pro2 天前
首个针对生物医药LLM智能体的全流程过程级评测框架
数据库·人工智能·windows·redis·量子计算