博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 缓存技术_java初探(1)之缓存技术
阅读量:6602 次
发布时间:2019-06-24

本文共 2480 字,大约阅读时间需要 8 分钟。

@Controller

@RequestMapping("/goods")public classGoodsController {

@AutowiredprivateMiaoshaUserService miaoshaUserService;

@AutowiredprivateGoodsService goodsService;

@AutowiredprivateRedisService redisService;

@AutowiredprivateThymeleafViewResolver thymeleafViewResolver;

@Autowired

ApplicationContext applicationContext;

@RequestMapping(value= "/to_list",produces = "text/html")

@ResponseBodypublicString list(Model model, MiaoshaUser user, HttpServletRequest request,HttpServletResponse response){

model.addAttribute("user",user);

String html= redisService.get(GoodsKey.getGoodsList, "", String.class);if(!StringUtils.isEmpty(html)){

System.out.println("从缓存中取");returnhtml;

}

List goodsList =goodsService.listGoodsVo();

model.addAttribute("goodsList",goodsList);

SpringWebContext ctx= newSpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(),applicationContext);//手动渲染

html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);if(!StringUtils.isEmpty(html)){

redisService.set(GoodsKey.getGoodsList,"",html);

}

System.out.println("从数据库中取");returnhtml;

}

@RequestMapping(value= "/to_detail/{goodsId}", produces = "text/html")

@ResponseBodypublicString detail(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user,

@PathVariable("goodsId") longgoodsId){

model.addAttribute("user",user);

String html=redisService.get(GoodsKey.getGoodsDetail,""+goodsId,String.class);if(!StringUtils.isEmpty(html)){

System.out.println("从缓存中取");returnhtml;

}

GoodsVo goods=goodsService.getGoodsVoByGoodsId(goodsId);

model.addAttribute("goods", goods);//得到秒杀的开始时间、结束时间、以及当前时间

long startAt =goods.getStartDate().getTime();long endAt =goods.getEndDate().getTime();long now =System.currentTimeMillis();//设置剩余时间

int remainSeconds=0;//设置秒杀状态

int miaoshaStatus=0;//判断

if(now

miaoshaStatus=0;

remainSeconds= (int) ((startAt-now)/1000);

}else if(now>endAt){//秒杀已经结束

miaoshaStatus=2;

remainSeconds=-1;

}else{//秒杀正在进行

miaoshaStatus=1;

remainSeconds=0;

}

model.addAttribute("miaoshaStatus",miaoshaStatus);

model.addAttribute("remainSeconds",remainSeconds);

SpringWebContext ctx= newSpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(),applicationContext);//手动渲染

html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", ctx);if(!StringUtils.isEmpty(html)){

redisService.set(GoodsKey.getGoodsDetail,""+goodsId,html);

}

System.out.println("从数据库中取");returnhtml;

}

}

转载地址:http://ferio.baihongyu.com/

你可能感兴趣的文章
nginx安装与配置2(转载)
查看>>
Linux下Mongodb安装和启动配置
查看>>
2015 成长计划
查看>>
沈阳一饭店凌晨爆燃,燃气报警器时刻预防
查看>>
Redis 与 数据库处理数据的两种模式
查看>>
VUE2中axios的使用方法
查看>>
assert 断言
查看>>
CS 229 notes Supervised Learning
查看>>
2018.10.27-dtoj-3996-Lesson5!(johnny)
查看>>
DataTable转换成json字符串
查看>>
RecyclerView重用导致的元素重复问题
查看>>
iOS网络协议----HTTP/TCP/IP浅析
查看>>
ubuntu 12.04 安装 redis
查看>>
IOS_CGRect
查看>>
Sql Server中不常用的表运算符之APPLY(1)
查看>>
【DM642】ICELL Interface—Cells as Algorithm Containers
查看>>
linux所有命令失效的解决办法
查看>>
力扣算法题—085最大矩阵
查看>>
svs 在创建的时候 上传文件夹 bin obj 这些不要提交
查看>>
mysql-用命令导出、导入表结构或数据
查看>>