分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

Ajax异步加载数据及Redis缓存

发布时间:2023-09-06 02:28责任编辑:赖小花关键词:Ajax缓存

针对网页分类条目的动态加载,图为页面的Head部分。

//categoryListServlet准备分类数据ProductService service = new ProductService();List<Category> categoryList = service.findAllCategoryList();response.setContentType("text/html; charset=utf-8");Gson gson = new Gson();String json = gson.toJson(categoryList);response.getWriter().write(json);

head.jsp异步加载js部分:

<script type="text/javascript"> ?????????//head.jsp加载完成后,ajax异步加载分类条 ?????????$(function(){ ???????????????????????????var content= ""; ?????????????$.post( ?????????????????????"${pageContext.request.contextPath}/categoryList", ?????????????????????function(data){ ?????????????????????????//[{"cid":"xxx","cname":"aaa"},{"cid":"xxx","cname":"aaa"}] ?????????????????????????for(var i=0;i<data.length;i++){ ?????????????????????????????content += "<li><a href=‘#‘>"+data[i].cname+"</a></li>"; ?????????????????????????} ?????????????????????????//将拼接好的类别,写入到页面 ?????????????????????????$("#categoryUI").html(content); ???????????????????????}, ?????????????????????"json" ?????????????); ???????????????????????}); ???????????????????????</script>

缓存逻辑:

  1.查询缓存中有无分类数据

  2.有,直接查询缓存;

   无,则通过hibernate查询,并添加到缓存中

  3.将查询到的数据返回。

//查询缓存中有无分类数据,如果没有查询写入缓存Jedis jedis = JedisPoolUtils.getJedis();String categoryListJson = jedis.get("categoryListJson");if(categoryListJson == null){ ???System.out.println("缓存没有数据 查询数据库"); ???ProductService service = new ProductService(); ???List<Category> categoryList = service.findAllCategoryList(); ???Gson gson = new Gson(); ???categoryListJson = gson.toJson(categoryList); ???jedis.set("categoryListJson", categoryListJson);}//准备分类数据 ???response.setContentType("text/html; charset=utf-8");response.getWriter().write(categoryListJson);

Ajax异步加载数据及Redis缓存

原文地址:https://www.cnblogs.com/zemul/p/10215978.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved