Ajax异步查询商品类别 使用缓存技术查询商品类别

本文介绍了一种通过引入Redis缓存技术来优化Ajax请求响应速度的方法。具体实现包括:利用JSP页面进行Ajax调用,通过Servlet处理业务逻辑并使用Gson进行JSON数据转换,同时借助Jedis连接Redis数据库实现数据缓存。此方案有效减少了数据库的访问频率,提升了用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

所需jar包:https://pan.baidu.com/s/1hgUDCnAm_L9Rij5j-JXTHQ

1.   jsp页面

<script type="text/javascript">
    
            $(function(){
                var content = "";
                $.post(
                    "${pageContext.request.contextPath}/product?method=findCategoryList",
                    function(data){
                        //动态获取<li><a href="#">${cate.cname }</a></li>
                        for(var i=0;i<data.length;i++){
                            content+="<li><a href='${pageContext.request.contextPath}/product?method=findCategoryInfo&cid="+data[i].cid+"'>"+data[i].cname+"</a></li>";
                        }
                        
                        //将拼接好的li放到ul中
                        $("#categoryUI").html(content);
                    },
                    "json"
                );
            });

    </script>

 

<ul class="nav navbar-nav" id="categoryUI">

</ul>

2.servlet页面

//Ajax异步查询商品类别
    public void findCategoryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductService dao = new ProductService();
        List<Category> categoryList = dao.findCategoryList();
        
        Gson gson = new Gson();
        String json = gson.toJson(categoryList);
        response.getWriter().write(json);

    }

3.service层,dao层   略

------------------------------------------------------使用缓存技术--------------

1.jsp页面同上

2.servlet页面

   public void findCategoryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用缓存技术
        //先从缓存中查找categoryListJson,如果有直接使用,如果没有再从数据库中查找
        //1 获得jedis对象  连接jedis数据库
        Jedis jedis = JedisPoolUtils.getJedis();
        String categoryListJson = jedis.get("categoryListJson");
        //2 判断categoryListJson是否为空
        if(categoryListJson == null) {
        //    System.out.println("从数据库中查询");
            //没有接受的参数直接调用service层
            ProductService service = new ProductService();
            //查找商品类别
            List<Category> categoryList = service.findCategoryList();

            //json转换
            Gson gson = new Gson();
            categoryListJson = gson.toJson(categoryList);
            jedis.set("categoryListJson", categoryListJson);
        }
        
        //解决中文乱码
        response.setContentType("text/html;charset=UTF-8");
        //将数据写回ajax

        response.getWriter().write(categoryListJson);

}

3.工具类JedisPoolUtils.java

package com.wenhao.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtils {
    
    private static JedisPool pool = null ;

    static {
        //加载配置文件
        InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
        Properties pro = new Properties();
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));  //最大闲置个数
        poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));  //最小闲置个数
        poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));  //最大连接数
        
        //1 创建Redis的连接池
        pool = new JedisPool(poolConfig, pro.getProperty("redis.url"), Integer.parseInt(pro.get("redis.port").toString()));
    }
    
    
    
    //获得jedis资源的方法
    public static Jedis getJedis() {
        return pool.getResource();
    }
    
    
    public static void main(String[] args) {
        Jedis jedis = getJedis();
        System.out.println(jedis.get("username"));
    }

}

4.redis.properties配置文件

redis.maxIdle=30
redis.minIdle=10
redis.maxTotal=50
redis.url=192.168.75.134
redis.port=6379


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值