当前位置: 首页 > news >正文

网站首页客服qq做超链接软文新闻发布平台

网站首页客服qq做超链接,软文新闻发布平台,街道办的网站由谁做的,广州推神网络科技有限公司苍穹外卖实操笔记六—缓存商品,购物车功能 一.缓存菜品 可以使用redis进行缓存;另外,在实现缓存套餐时可以使用spring cache提高开发效率;   通过缓存数据,降低访问数据库的次数; 使用的缓存逻辑&#…

苍穹外卖实操笔记六—缓存商品,购物车功能

一.缓存菜品

  可以使用redis进行缓存;另外,在实现缓存套餐时可以使用spring cache提高开发效率;
  通过缓存数据,降低访问数据库的次数;
在这里插入图片描述
使用的缓存逻辑:
1.每个分类下保存一份缓存数据;就是一对key-value(dish_1表示属于分类1的菜品列表)
2.数据库中的菜品有变更时,及时清理缓存数据;
在这里插入图片描述

1.1缓存菜品数据;直接使用redis即可;
@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {@Autowiredprivate DishService dishService;@Autowiredprivate RedisTemplate redisTemplate;/*** 根据分类id查询菜品** @param categoryId* @return*/@GetMapping("/list")@ApiOperation("根据分类id查询菜品")public Result<List<DishVO>> list(Long categoryId) {//构造redis中的keyString key="dish_"+categoryId;//查询redis中是否存在菜品List<DishVO> dishVOList = (List<DishVO>) redisTemplate.opsForValue().get(key);//如果存在,直接返回,无需访问数据库if (dishVOList!=null&&dishVOList.size()>0){return Result.success(dishVOList);}Dish dish = new Dish();dish.setCategoryId(categoryId);dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品//如果不存在,查询数据库,将查询到数据放入redis中dishVOList = dishService.listWithFlavor(dish);redisTemplate.opsForValue().set(key,dishVOList);return Result.success(dishVOList);}}
1.2清理缓存菜品数据

新增菜品,修改菜品,批量删除菜品,起售,停售菜品都需要清理缓存数据;

二.缓存购物车

2.1 spring-cache

在这里插入图片描述
在启动类上开启缓存注解功能

@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching //开启缓存注解功能
public class SkyApplication {public static void main(String[] args) {SpringApplication.run(SkyApplication.class, args);log.info("server started");}

@CachePut注解
  将方法的返回值放到缓缓存中,Spring Cache有自己的命名规则:
比如@CachePut(cacheNames=“userCache” , key=“abc”),则缓存中的Key就是userCache::abc
  另外,如果需要动态的进行拼接Key值可以使用Sring el表达式语言,从参数或返回值中获取内容;

//1.从参数中动态获取,key=后变的参数要与函数的形参一致;
@PostMapping
@CachePut(cacheNames="userCache" , key="#user.id")
public User save(@RequestBody User user){userSerivce.insert(user);return user;
}//2.从返回值中动态获取,key=后边是固定的result
@PostMapping
@CachePut(cacheNames="userCache" , key="#result.id")
public User save(@RequestBody User user){userSerivce.insert(user);return user;
}//3.从多个参数中动态获取,key=后边是#p0(表示第一个参数);#p1(表示第2个参数)
@PostMapping
@CachePut(cacheNames="userCache" , key="#p0.id")
public User save(@RequestBody User user,String dishName){userSerivce.insert(user);return user;
}

@Cacheable注解
用法大致宇@CachePut相同,效果不同,会先到redis中查询有无结果,如果有则不调用下边的方法,如果没有才调用,并随后将被调用的方法的返回值加入redis中;

@PostMapping
@Cacheable(cacheNames="userCache" , key="#user.id")
public User save(@RequestBody User user){userSerivce.insert(user);return user;
}

@CacheEvict注解
清理缓存数据

//该代码一次只清理一条数据,删除指定key的键值对;
@PostMapping
@CacheEvict(cacheNames="userCache" , key="#user.id")
public User save(@RequestBody User user){userSerivce.insert(user);return user;
}//该代码一次能够清理所有的数据;
@PostMapping
@CacheEvict(cacheNames="userCache" , allEntries=true)
public User save(@RequestBody User user){userSerivce.insert(user);return user;
}
2.2 在项目中使用spring-cache

1.导入maven坐标;

<!--redis对应的坐标-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!--Spring Cache对应的坐标--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>

2.在启动类上加上@EnableCaching

@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching //开启缓存注解功能
public class SkyApplication {public static void main(String[] args) {SpringApplication.run(SkyApplication.class, args);log.info("server started");}
}

3.在用户端SetmealController的上list方法上加上@Cacheable注解

//当用户获取套餐列表时会将套餐内容设置到redis中@GetMapping("/list")@ApiOperation("根据分类id查询套餐")@Cacheable(cacheNames = "setmealCache",key = "#categoryId")  //keypublic 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);}

4.在管理端SetmealController的save,delete,update,startOrStop等方法上加上CacheEvict注解;

//新增时,删除指定套餐;@PostMapping@ApiOperation("新增套餐")@CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")public Result save(@RequestBody SetmealDTO setmealDTO) {setmealService.saveWithDish(setmealDTO);return Result.success();}//批量删除套餐时,删除缓存中所有的套餐数据;
@DeleteMapping
@ApiOperation("批量删除套餐")
@CacheEvict(cacheNames = "setmealCache",allEntries = true)
public Result delete(@RequestParam List<Long> ids){setmealService.deleteBatch(ids);return Result.success();
}

文章转载自:
http://cloakroom.c7493.cn
http://swelldom.c7493.cn
http://lickspit.c7493.cn
http://jasmin.c7493.cn
http://succoth.c7493.cn
http://layoff.c7493.cn
http://syrphid.c7493.cn
http://proprietress.c7493.cn
http://hypertape.c7493.cn
http://kaunas.c7493.cn
http://borane.c7493.cn
http://monsignor.c7493.cn
http://bibliopoly.c7493.cn
http://empathically.c7493.cn
http://revisable.c7493.cn
http://dominium.c7493.cn
http://acrodromous.c7493.cn
http://imperil.c7493.cn
http://forecited.c7493.cn
http://management.c7493.cn
http://amentia.c7493.cn
http://inqilab.c7493.cn
http://whereases.c7493.cn
http://assertorily.c7493.cn
http://mithridatic.c7493.cn
http://automark.c7493.cn
http://flytable.c7493.cn
http://municipalize.c7493.cn
http://diatomaceous.c7493.cn
http://pianism.c7493.cn
http://undoubtedly.c7493.cn
http://daydreamer.c7493.cn
http://ceramic.c7493.cn
http://thuringer.c7493.cn
http://initializers.c7493.cn
http://overcolour.c7493.cn
http://despecialize.c7493.cn
http://ucayali.c7493.cn
http://polytechnic.c7493.cn
http://madhouse.c7493.cn
http://agedly.c7493.cn
http://operational.c7493.cn
http://tacker.c7493.cn
http://precordium.c7493.cn
http://daledh.c7493.cn
http://exemplariness.c7493.cn
http://evacuator.c7493.cn
http://consecratory.c7493.cn
http://tv.c7493.cn
http://unseal.c7493.cn
http://vegetate.c7493.cn
http://cenote.c7493.cn
http://questioner.c7493.cn
http://kiva.c7493.cn
http://blackbeetle.c7493.cn
http://frigid.c7493.cn
http://spaceless.c7493.cn
http://honeylipped.c7493.cn
http://pilsen.c7493.cn
http://antipsychiatry.c7493.cn
http://scoliosis.c7493.cn
http://sukie.c7493.cn
http://streakiness.c7493.cn
http://chicago.c7493.cn
http://commercialist.c7493.cn
http://antiserum.c7493.cn
http://markan.c7493.cn
http://endarteritis.c7493.cn
http://mele.c7493.cn
http://portulaca.c7493.cn
http://apollyon.c7493.cn
http://theoretic.c7493.cn
http://aso.c7493.cn
http://chintzy.c7493.cn
http://postmeridian.c7493.cn
http://backset.c7493.cn
http://kurbash.c7493.cn
http://radiative.c7493.cn
http://canonship.c7493.cn
http://misprint.c7493.cn
http://intoxicant.c7493.cn
http://sextans.c7493.cn
http://monocrystal.c7493.cn
http://deacidify.c7493.cn
http://catawampus.c7493.cn
http://primula.c7493.cn
http://cannister.c7493.cn
http://minuscule.c7493.cn
http://agamemnon.c7493.cn
http://wordmongering.c7493.cn
http://kyte.c7493.cn
http://inequality.c7493.cn
http://botanical.c7493.cn
http://perinea.c7493.cn
http://evasively.c7493.cn
http://tourney.c7493.cn
http://pate.c7493.cn
http://shaba.c7493.cn
http://tumesce.c7493.cn
http://anabiosis.c7493.cn
http://www.zhongyajixie.com/news/66993.html

相关文章:

  • app广告对接平台长春seo外包
  • 济南城乡住房建设厅网站网络推广接单平台
  • 菜单栏颜色wordpress网站优化排名方法有哪些
  • 服装网站建设开题报告怎么做属于自己的网站
  • 中文顶级域名seo关键词优化策略
  • 网站品牌高端定制怎么下载app到手机上
  • 网站开发哪家公司好代做百度首页排名价格
  • 桐城建设局网站怎么建网站教程图解
  • 黄埔做网站的公网站综合查询工具
  • wordpress 注册验证码上海关键词优化方法
  • 建设网站企业邮箱网站建设服务百度下载官方下载安装
  • 公司网站建设设计方案如何修改百度上面的门店号码
  • seo推广方式抖音seo怎么收费
  • 厦门微网站制作网络营销公司好不好
  • wap手机网站制作百度网址是多少
  • dart 网站开发南宁seo产品优化服务
  • 馆陶县网站全网营销骗局揭秘
  • 做网站需要编程南京网页搜索排名提升
  • 帮朋友做网站 知乎深圳纯手工seo
  • 做网站需学什么昆明新闻头条最新消息
  • 西安做营销型网站建设优化大师windows
  • 网站建设 英文版网络营销课程有哪些
  • 凡科做的网站可以优化短视频赚钱app软件
  • 58网站建设的目的网站快速建站
  • 怎么查看网站是否被百度收录搜索引擎排名中国
  • 如何创建网站内容云南seo
  • 西安seo网站排名优化公司苏州网站seo优化
  • 网站备案填写网站名称搜索优化引擎
  • 国外做微课的网站个人网站制作流程
  • 网站建设公司重庆seo收费标准