package com.hmdp.service.impl;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.hmdp.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author WuZhiQiang
* @since 2022-11-8
*/
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public Result queryTypeList() {
//先查缓存redis是否有数据
//!!!!!!
//!!!!!!这里的RedisConstants.CACHE_SHOP_TYPE 是 "cache:shop-type";
//!!!!!!
//debug发现取出的10条数据全在一起,也
13 条评论
您还未登录,请先
登录
后发表或查看评论
13 条评论
-
我和寂世 2023.03.01shopTypeController里面: @GetMapping("list") public Result queryTypeList() { if (typeService.queryByList() == null) { return Result.fail("分类错误!未找到"); } return Result.ok(typeService.queryByList()); }
-
我和寂世 2023.03.01//5. 数据库有,使用Redis缓存,并返回 //返回前将List<ShopType>转为List<String> List<String> typeListToStrings = typeList.stream().map(shopType -> { return JSONUtil.toJsonStr(shopType); }).collect(Collectors.toList()); stringRedisTemplate.opsForList().leftPushAll(CACHE_SHOP_TYPE_KEY, typeListToStrings); return typeList; -
-
我和寂世回复我和寂世 2023.03.01这里的Service和ServiceImpl的返回值类型改为List<ShopType>,不用Result,统一将数据交给Controller来Result
-
-
我和寂世 2023.03.01//1. redis是否有缓存 Long size = stringRedisTemplate.opsForList().size(CACHE_SHOP_TYPE_KEY); //2. 有,直接返回 if (size != 0) { List<String> typeList = stringRedisTemplate.opsForList().range(CACHE_SHOP_TYPE_KEY, 0, size); //返回前将List<String>转为List<ShopType> List<ShopType> shopTypes = typeList.stream().map(s -> { ShopType shopType = JSONUtil.toBean(s, ShopType.class); return shopType; }).collect(Collectors.toList()); return shopTypes; } //3. 没有,查数据库 List<ShopType> typeList = this.query().orderByAsc("sort").list(); if (typeList == null || typeList.isEmpty()) { //4. 数据库没有,直接返回错误 // return Result.fail("分类错误!未找到"); return null; }

最低0.47元/天 解锁文章





