国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

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

松山湖仿做網(wǎng)站,關(guān)聯(lián)詞有哪些四年級(jí),外貿(mào)網(wǎng)站建設(shè)推廣公司價(jià)格,佛山網(wǎng)站制作公司一、緩存菜品 通過(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ù),然后載入緩存 將菜品集合序列化后緩存入red…

一、緩存菜品

通過(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);

http://m.aloenet.com.cn/news/37987.html

相關(guān)文章:

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