黑马Redis教程-课后练习

黑马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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值