黑马Redis教程-课后练习
P37课后练习,采用的以String存储数据
ShopTypeController
package com.hmdp.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.service.IShopTypeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/shop-type")
public class ShopTypeController {
@Resource
private IShopTypeService typeService;
@GetMapping("list")
public Result queryTypeList() throws JsonProcessingException {
return typeService.queryTypeList();
}
}
IShopTypeService
package com.hmdp.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.baomidou.mybatisplus.extension.service.IService;
/**
*
* 服务类
*/
public interface IShopTypeService extends IService<ShopType> {
Result queryTypeList() throws JsonProcessingException;
}
ShopTypeServiceImpl
需要自己改下CACHE_SHOP_TYPE_TTl,改为合理的时间,或者删掉也行
package com.hmdp.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static com.hmdp.constants.RedisConstants.CACHE_SHOP_TYPE;
import static com.hmdp.constants.RedisConstants.CACHE_SHOP_TYPE_TTl;
/**
*
* 服务实现类
*/
@Slf4j
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private ShopTypeMapper shopTypeMapper;
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public Result queryTypeList() throws JsonProcessingException {
// 1.从redis中取出list
String stringShop = stringRedisTemplate.opsForValue().get(CACHE_SHOP_TYPE);
// 反序列化
if(stringShop != null && !stringShop.isEmpty()) {
// 2.能够从redis中查出来,并返回
List<ShopType> shopTypes = mapper.readValue(stringShop, mapper.getTypeFactory()
.constructCollectionType(List.class, ShopType.class));
log.info("成功从redis中取出数据");
return Result.ok(shopTypes);
}
// 3.redis中不存在数据,从数据库中查询
List<ShopType> shopTypes = shopTypeMapper.selectList(new QueryWrapper<>());
// 4.在数据库中未命中,返回
if (shopTypes.isEmpty()) {
return Result.fail("没有数据");
}
// 5.数据库中未命中,将数据保存到redis中,返回
// 序列化
String stringShopType = mapper.writeValueAsString(shopTypes);
stringRedisTemplate.opsForValue().set(CACHE_SHOP_TYPE, stringShopType, CACHE_SHOP_TYPE_TTl, TimeUnit.MINUTES);
return Result.ok(shopTypes);
}
}