zoukankan      html  css  js  c++  java
  • Ajax异步加载数据及Redis缓存

    针对网页分类条目的动态加载,图为页面的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);
  • 相关阅读:
    对websoceket进行压力测试(一)
    学习springboot的一个网站
    重装mysql数据库
    websocket扫盲:基础知识(二)
    json-lib 之jsonConfig详细使用
    hibernate的like用法(用占位符解决)
    【转载】hibernate查询参数绑定
    Struts2 Anotation action
    PLSQL怎样导出oracle表结构
    从命令行启动oracle服务
  • 原文地址:https://www.cnblogs.com/zemul/p/10215978.html
Copyright © 2011-2022 走看看