松山湖仿做網(wǎng)站關(guān)聯(lián)詞有哪些四年級(jí)
一、緩存菜品
通過(guò)緩存的方式提高查詢性能
1.1問(wèn)題說(shuō)明
大量的用戶訪問(wèn)導(dǎo)致數(shù)據(jù)庫(kù)訪問(wèn)壓力增大,造成系統(tǒng)響應(yīng)慢,用戶體驗(yàn)差
1.2 實(shí)現(xiàn)思路
優(yōu)先查詢緩存,如果緩存沒(méi)有再去查詢數(shù)據(jù)庫(kù),然后載入緩存
將菜品集合序列化后緩存入redis? key為每個(gè)分類的id
1.3 代碼開(kāi)發(fā)(緩存菜品)
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品瀏覽接口")
public class DishController {@Autowiredprivate DishService dishService;@Autowiredprivate RedisTemplate redisTemplate;/*** 根據(jù)分類id查詢菜品** @param categoryId* @return*/@GetMapping("/list")@ApiOperation("根據(jù)分類id查詢菜品")public Result<List<DishVO>> list(Long categoryId) {//構(gòu)造redis中的key,規(guī)則:dish_分類idString key ="dish_"+categoryId;//查詢r(jià)edis中是否存在菜品數(shù)據(jù)List<DishVO> list =(List<DishVO>) redisTemplate.opsForValue().get(key);if (list!=null&&list.size()>0){//如果存在,直接返回,無(wú)需查詢數(shù)據(jù)庫(kù)return Result.success(list);}//如果不存在,查詢數(shù)據(jù)庫(kù),將查詢到的數(shù)據(jù)放入redis中Dish dish = new Dish();dish.setCategoryId(categoryId);dish.setStatus(StatusConstant.ENABLE);//查詢起售中的菜品list = dishService.listWithFlavor(dish);redisTemplate.opsForValue().set(key,list);return Result.success(list);}}
1.4 代碼開(kāi)發(fā)(清除緩存)
為什么要清除緩存? 為了保證數(shù)據(jù)的一致性,數(shù)據(jù)庫(kù)修改后,緩存中的數(shù)據(jù)并沒(méi)有發(fā)生改變,用戶再次請(qǐng)求后不會(huì)請(qǐng)求到新修改的數(shù)據(jù),而是過(guò)期的數(shù)據(jù)所以要清除緩存。
什么時(shí)候清除緩存? 后端修改數(shù)據(jù)后對(duì)緩存進(jìn)行及時(shí)的清除
/*** 統(tǒng)一清除緩存數(shù)據(jù)* @param pattern*/private void cleanCache(String pattern){Set keys = redisTemplate.keys(pattern);redisTemplate.delete(keys);}}
/*** 啟用禁用菜品* @param status* @param id* @return*/@PostMapping("/status/{status}")@ApiOperation("啟用禁用菜品")public Result startOrStop(@PathVariable Integer status,Long id){log.info("啟用禁用菜品,{},{}",status,id);dishService.startOrStop(status,id);//將所有的菜品緩存數(shù)據(jù)清理掉,所有以dish_開(kāi)頭的key
// Set keys = redisTemplate.keys("dish_");
// redisTemplate.delete(keys);cleanCache("dish_*");return Result.success();}
二、緩存套餐(基于SpringCache)
2.1 SpringCache
-
被寫(xiě)死了(沒(méi)有意義) @CachePut(cacheNames ="userCache",key = "abc")//如果使用SpringCache緩存數(shù)據(jù),key的生成:userCache::abc //生成的key 與cacheNames,key有關(guān)系
-
動(dòng)態(tài)生成key + SpEL表達(dá)式 @CachePut(cacheNames ="userCache",key = "#user.id")//如果使用SpringCache緩存數(shù)據(jù),key的生成:userCache::#user.id
2.2 入門(mén)案例
1.CachePut:將方法返回值放到緩存中
// @CachePut(cacheNames ="userCache",key = "#user.id")//如果使用SpringCache緩存數(shù)據(jù),key的生成:userCache::abc@PostMapping
// @CachePut(cacheNames = "userCache",key ="#result.id")//對(duì)象導(dǎo)航
// @CachePut(cacheNames = "userCache",key = "#p0.id")//#p0代表第一個(gè)參數(shù)
// @CachePut(cacheNames = "userCache",key = "#a0.id")@CachePut(cacheNames = "userCache",key = "#root.args[0].id")//#root.args[0]代表第一個(gè)參數(shù)public User save(@RequestBody User user){userMapper.insert(user);return user;}
2.Cacheable:在方法執(zhí)行前先查詢緩存,如果緩存中有該數(shù)據(jù),直接返回緩存中的數(shù)據(jù)。如果沒(méi)有在方法執(zhí)行后再將返回值放入緩存中
@GetMapping@Cacheable(cacheNames = "userCache",key = "#id")public User getById(Long id){User user = userMapper.getById(id);return user;}
Cacheable 不能使用#result.id這種方式設(shè)置key的值?
3.CacheEvict :將一條或者多條數(shù)據(jù)從緩存中刪除
- 刪除單條數(shù)據(jù)
@CacheEvict(cacheNames = "userCache",key ="#id" )@DeleteMappingpublic void deleteById(Long id){userMapper.deleteById(id);}
- 刪除多條數(shù)據(jù)(allEntries = true)
@CacheEvict(cacheNames = "userCache",allEntries = true)@DeleteMapping("/delAll")public void deleteAll(){userMapper.deleteAll();}
2.3實(shí)現(xiàn)思路
2.4代碼開(kāi)發(fā)
import com.sky.constant.StatusConstant;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
@Api(tags = "C端-套餐瀏覽接口")
public class SetmealController {@Autowiredprivate SetmealService setmealService;/*** 條件查詢** @param categoryId* @return*/@Cacheable(cacheNames = "setmealCache",key = "#categoryId")//key :setmealCache::100@GetMapping("/list")@ApiOperation("根據(jù)分類id查詢套餐")public Result<List<Setmeal>> list(Long categoryId) {Setmeal setmeal = new Setmeal();setmeal.setCategoryId(categoryId);setmeal.setStatus(StatusConstant.ENABLE);List<Setmeal> list = setmealService.list(setmeal);return Result.success(list);}/*** 根據(jù)套餐id查詢包含的菜品列表** @param id* @return*/@GetMapping("/dish/{id}")@ApiOperation("根據(jù)套餐id查詢包含的菜品列表")public Result<List<DishItemVO>> dishList(@PathVariable("id") Long id) {List<DishItemVO> list = setmealService.getDishItemById(id);return Result.success(list);}
}
import com.github.pagehelper.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;import java.util.List;/*** 套餐管理*/
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理相關(guān)接口")
@Slf4j
public class SetmealController {@Autowiredprivate SetmealService setmealService;@CacheEvict(cacheNames = "setmealCache",key = "setmealDTO.categoryId")@PostMapping@ApiOperation("新增套餐接口")public Result save(@RequestBody SetmealDTO setmealDTO){log.info("新增套餐:{}",setmealDTO);setmealService.saveWithDish(setmealDTO);return Result.success();}/*** 套餐分頁(yè)查詢* @param setmealPageQueryDTO* @return*/@GetMapping("/page")@ApiOperation("套餐分頁(yè)查詢")public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){log.info("套餐分頁(yè)查詢:{}",setmealPageQueryDTO);PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);return Result.success(pageResult);}/*** 批量刪除套餐* @param ids* @return*/@CacheEvict(cacheNames = "setmealCache",allEntries = true)@DeleteMapping@ApiOperation("批量刪除菜品")public Result delete(@RequestParam List<Long> ids){log.info("批量刪除套餐:{}",ids);setmealService.deleteBatch(ids);return Result.success();}/*** 根據(jù)id查詢套餐,用于修改頁(yè)面回顯數(shù)據(jù)** @param id* @return*/@GetMapping("/{id}")@ApiOperation("根據(jù)id查詢套餐")public Result<SetmealVO> getById(@PathVariable Long id) {SetmealVO setmealVO = setmealService.getByIdWithDish(id);return Result.success(setmealVO);}/*** 修改套餐** @param setmealDTO* @return*/@CacheEvict(cacheNames = "setmealCache",allEntries = true)@PutMapping@ApiOperation("修改套餐")public Result update(@RequestBody SetmealDTO setmealDTO) {setmealService.update(setmealDTO);return Result.success();}/*** 套餐起售停售* @param status* @param id* @return*/@CacheEvict(cacheNames = "setmealCache",allEntries = true)@PostMapping("/status/{status}")@ApiOperation("套餐起售停售")public Result startOrStop(@PathVariable Integer status, Long id) {setmealService.startOrStop(status, id);return Result.success();}}
三、添加購(gòu)物車
3.1需求分析和設(shè)計(jì)
3.2 代碼開(kāi)發(fā)
3.2.1Controller
import com.sky.dto.ShoppingCartDTO;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "C端購(gòu)物車相關(guān)接口")
@Slf4j
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;/*** 添加購(gòu)物車* @param shoppingCartDTO* @return*/@PostMapping("/add")@ApiOperation("添加購(gòu)物車")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){log.info("添加購(gòu)物車,商品信息為:",shoppingCartDTO);shoppingCartService.addShoppingCart(shoppingCartDTO);return Result.success();}}
?3.2.2 Serveice層
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {@Autowiredprivate ShoppingCartMapper shoppingCartMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 添加購(gòu)物車* @param shoppingCartDTO*/@Overridepublic void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {//判斷當(dāng)前加入到購(gòu)物車中的商品是否已經(jīng)存在了ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);Long userId = BaseContext.getCurrentId();shoppingCart.setUserId(userId);List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//如果已經(jīng)存在了,只需要將其數(shù)量加1if (list !=null && list.size()>0){ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber()+1);shoppingCartMapper.updateNumberById(cart);}else {//如果不存在,需要插入一條購(gòu)物車數(shù)據(jù)//判斷本次添加購(gòu)物車的是菜品還是套餐Long dishId = shoppingCartDTO.getDishId();if (dishId !=null){//本次添加的是菜品Dish dish = dishMapper.getById(dishId);shoppingCart.setName(dish.getName());shoppingCart.setImage(dish.getImage());shoppingCart.setAmount(dish.getPrice());
// shoppingCart.setNumber(1);
// shoppingCart.setCreateTime(LocalDateTime.now());
//}else {//本次添加的是套餐Long setmealId = shoppingCartDTO.getSetmealId();Setmeal setmeal = setmealMapper.getById(setmealId);shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());
// shoppingCart.setNumber(1);
// shoppingCart.setCreateTime(LocalDateTime.now());}shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}
}
四、查看購(gòu)物車
4.1 需求分析和設(shè)計(jì)
4.2 代碼開(kāi)發(fā)
4.2.1Controller層
/*** 查看購(gòu)物車* @return*/@GetMapping("/list")@ApiOperation("查看購(gòu)物車")public Result<List<ShoppingCart>> list(){List<ShoppingCart> list = shoppingCartService.showShoppingCart();return Result.success(list);}
4.2.2 Service層
/*** 查看購(gòu)物車* @return*/@Overridepublic List<ShoppingCart> showShoppingCart() {//獲取當(dāng)前微信用戶的idLong userId = BaseContext.getCurrentId();ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);return list;}
五、清空購(gòu)物車
5.1 需求分析和設(shè)計(jì)
5.2 代碼開(kāi)發(fā)
5.2.1 Controller層
/*** 清空購(gòu)物車* @return*/@DeleteMapping("/clean")public Result clean(){shoppingCartService.cleanShoppingCart();return Result.success();}
5.2.2 Service層
/*** 清空購(gòu)物車*/@Overridepublic void cleanShoppingCart() {Long userId = BaseContext.getCurrentId();shoppingCartMapper.deleteByUserId(userId);}
5.2.3 Mapper層
/*** 根據(jù)用戶id刪除購(gòu)物車數(shù)據(jù)* @param userId*/@Delete("delete from shopping_cart where user_id =#{userId}")void deleteByUserId(Long userId);