中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

松山湖仿做網(wǎng)站關聯(lián)詞有哪些四年級

松山湖仿做網(wǎng)站,關聯(lián)詞有哪些四年級,外貿網(wǎng)站建設推廣公司價格,佛山網(wǎng)站制作公司一、緩存菜品 通過緩存的方式提高查詢性能 1.1問題說明 大量的用戶訪問導致數(shù)據(jù)庫訪問壓力增大,造成系統(tǒng)響應慢,用戶體驗差 1.2 實現(xiàn)思路 優(yōu)先查詢緩存,如果緩存沒有再去查詢數(shù)據(jù)庫,然后載入緩存 將菜品集合序列化后緩存入red…

一、緩存菜品

通過緩存的方式提高查詢性能

1.1問題說明

大量的用戶訪問導致數(shù)據(jù)庫訪問壓力增大,造成系統(tǒng)響應慢,用戶體驗差

1.2 實現(xiàn)思路

優(yōu)先查詢緩存,如果緩存沒有再去查詢數(shù)據(jù)庫,然后載入緩存

將菜品集合序列化后緩存入redis? key為每個分類的id

1.3 代碼開發(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) {//構造redis中的key,規(guī)則:dish_分類idString key ="dish_"+categoryId;//查詢redis中是否存在菜品數(shù)據(jù)List<DishVO> list =(List<DishVO>) redisTemplate.opsForValue().get(key);if (list!=null&&list.size()>0){//如果存在,直接返回,無需查詢數(shù)據(jù)庫return Result.success(list);}//如果不存在,查詢數(shù)據(jù)庫,將查詢到的數(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 代碼開發(fā)(清除緩存)

為什么要清除緩存? 為了保證數(shù)據(jù)的一致性,數(shù)據(jù)庫修改后,緩存中的數(shù)據(jù)并沒有發(fā)生改變,用戶再次請求后不會請求到新修改的數(shù)據(jù),而是過期的數(shù)據(jù)所以要清除緩存。

什么時候清除緩存? 后端修改數(shù)據(jù)后對緩存進行及時的清除

    /*** 統(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_開頭的key
//        Set keys = redisTemplate.keys("dish_");
//        redisTemplate.delete(keys);cleanCache("dish_*");return Result.success();}

二、緩存套餐(基于SpringCache)

2.1 SpringCache

  • 被寫死了(沒有意義)
    @CachePut(cacheNames ="userCache",key = "abc")//如果使用SpringCache緩存數(shù)據(jù),key的生成:userCache::abc            //生成的key 與cacheNames,key有關系 
  • 動態(tài)生成key  + SpEL表達式
    @CachePut(cacheNames ="userCache",key = "#user.id")//如果使用SpringCache緩存數(shù)據(jù),key的生成:userCache::#user.id

2.2 入門案例

1.CachePut:將方法返回值放到緩存中

//    @CachePut(cacheNames ="userCache",key = "#user.id")//如果使用SpringCache緩存數(shù)據(jù),key的生成:userCache::abc@PostMapping
//    @CachePut(cacheNames = "userCache",key ="#result.id")//對象導航
//    @CachePut(cacheNames = "userCache",key = "#p0.id")//#p0代表第一個參數(shù)
//    @CachePut(cacheNames = "userCache",key = "#a0.id")@CachePut(cacheNames = "userCache",key = "#root.args[0].id")//#root.args[0]代表第一個參數(shù)public User save(@RequestBody User user){userMapper.insert(user);return user;}

2.Cacheable:在方法執(zhí)行前先查詢緩存,如果緩存中有該數(shù)據(jù),直接返回緩存中的數(shù)據(jù)。如果沒有在方法執(zhí)行后再將返回值放入緩存中

    @GetMapping@Cacheable(cacheNames = "userCache",key = "#id")public User getById(Long id){User user = userMapper.getById(id);return user;}

Cacheable 不能使用#result.id這種方式設置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實現(xiàn)思路

2.4代碼開發(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 = "套餐管理相關接口")
@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();}/*** 套餐分頁查詢* @param setmealPageQueryDTO* @return*/@GetMapping("/page")@ApiOperation("套餐分頁查詢")public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){log.info("套餐分頁查詢:{}",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查詢套餐,用于修改頁面回顯數(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();}}

三、添加購物車

3.1需求分析和設計

3.2 代碼開發(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端購物車相關接口")
@Slf4j
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;/*** 添加購物車* @param shoppingCartDTO* @return*/@PostMapping("/add")@ApiOperation("添加購物車")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){log.info("添加購物車,商品信息為:",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;/*** 添加購物車* @param shoppingCartDTO*/@Overridepublic void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {//判斷當前加入到購物車中的商品是否已經(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 {//如果不存在,需要插入一條購物車數(shù)據(jù)//判斷本次添加購物車的是菜品還是套餐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);}}
}

四、查看購物車

4.1 需求分析和設計

4.2 代碼開發(fā)

4.2.1Controller層

/*** 查看購物車* @return*/@GetMapping("/list")@ApiOperation("查看購物車")public  Result<List<ShoppingCart>> list(){List<ShoppingCart> list = shoppingCartService.showShoppingCart();return  Result.success(list);}

4.2.2 Service層

    /*** 查看購物車* @return*/@Overridepublic List<ShoppingCart> showShoppingCart() {//獲取當前微信用戶的idLong userId = BaseContext.getCurrentId();ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);return list;}

五、清空購物車

5.1 需求分析和設計

5.2 代碼開發(fā)

5.2.1 Controller層

    /*** 清空購物車* @return*/@DeleteMapping("/clean")public  Result clean(){shoppingCartService.cleanShoppingCart();return Result.success();}

5.2.2 Service層

    /*** 清空購物車*/@Overridepublic void cleanShoppingCart() {Long userId = BaseContext.getCurrentId();shoppingCartMapper.deleteByUserId(userId);}

5.2.3 Mapper層

    /*** 根據(jù)用戶id刪除購物車數(shù)據(jù)* @param userId*/@Delete("delete from shopping_cart where user_id =#{userId}")void deleteByUserId(Long userId);

http://m.risenshineclean.com/news/37987.html

相關文章:

  • 中國建設銀行官網(wǎng)站代發(fā)工資濰坊關鍵詞優(yōu)化軟件
  • 中國建設銀行濟南招聘信息網(wǎng)站google搜索app下載
  • WordPress金融網(wǎng)站seo課程排行榜
  • 視頻網(wǎng)站如何做seo如何做電商賺錢
  • 四川省建設信息網(wǎng)站貴州seo學校
  • 南昌網(wǎng)站建設培訓班seo優(yōu)化基礎教程pdf
  • qq整人網(wǎng)站怎么做百度首頁百度一下
  • 新鄉(xiāng)營銷型網(wǎng)站建設產(chǎn)品營銷推廣策略
  • 百度站長怎么做網(wǎng)站維護二級域名查詢入口
  • 一學一做看視頻網(wǎng)站網(wǎng)站推廣的渠道有哪些
  • 做網(wǎng)站頁面該建多大的畫布以圖搜圖百度識圖
  • 做網(wǎng)站用java還是c語言專業(yè)推廣引流團隊
  • 網(wǎng)站維護一般多久上海優(yōu)化外包
  • 法院文化建設網(wǎng)站女孩短期技能培訓班
  • 網(wǎng)站后臺管理系統(tǒng)制作教程長春網(wǎng)站優(yōu)化哪家好
  • 營銷型網(wǎng)站制作哪家好網(wǎng)絡營銷的特點
  • 網(wǎng)站建設方案及預算百度上做優(yōu)化一年多少錢
  • 湛江網(wǎng)站的建設網(wǎng)站關鍵詞優(yōu)化推廣哪家快
  • 網(wǎng)站中的qq客服怎么做班級優(yōu)化大師是干什么用的
  • 如何推廣運營網(wǎng)站百度付費推廣
  • 網(wǎng)站建設頭部代碼網(wǎng)站描述和關鍵詞怎么寫
  • 長沙私人做網(wǎng)站現(xiàn)在推廣平臺哪家最好
  • wordpress和emlog重慶seo和網(wǎng)絡推廣
  • 網(wǎng)站開發(fā)文檔管理工具韓國網(wǎng)站
  • 淄博網(wǎng)站建設相關文章如何快速推廣
  • 天津做網(wǎng)站優(yōu)化公司上海網(wǎng)絡推廣優(yōu)化公司
  • 如何在網(wǎng)站后臺找到死鏈接群站優(yōu)化之鏈輪模式
  • 老河口做網(wǎng)站免費的外貿b2b網(wǎng)站
  • 單位網(wǎng)站建設工作功勞網(wǎng)絡營銷策劃書包括哪些內容
  • 湖南城鄉(xiāng)建設網(wǎng)站全網(wǎng)絡品牌推廣