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

商铺缓存

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);
    }
}
相关推荐
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.1 天前
《redis-cluster 集群部署完全手册(含扩容+缩容)》
数据库·redis·缓存
0xDevNull1 天前
Java项目中Redis热点Key自动检测方案详细教程
java·spring boot·redis
spencer_tseng1 天前
redis.windows.conf 2026.04.27
windows·redis
八秒记忆的老男孩1 天前
Sentinel5P的L1B级数据预处理(BD7和BD8)【20260427】
数据库·redis·缓存
OYangxf1 天前
基于epoll的单线程Reactor:Tinyredis的网络层实现
c++·redis
snow@li1 天前
数据库-Oracle:常用语法 / Oracle 核心知识技能梳理
数据库·redis·缓存
zhougl9961 天前
Redis 防止丢数据
java·redis·mybatis
星辰_mya1 天前
系统里的“特种部队”——缓存
缓存
snow@li1 天前
数据库-Redis:常用语法 / Redis 核心知识技能梳理
数据库·redis·缓存
aLTttY1 天前
Spring Boot + Redis 实战分布式锁:从入门到精通
spring boot·redis·分布式