• 案例20-页面使用redis缓存显示类别菜单


    1 准备工作

    1  需要导入所需要的jar包。

    2 启动windows版本的redis服务端

    3 准备JedisUtils工具类的配置文件redis.properties

    redis.maxIdle=30
    redis.minIdle=10
    redis.maxTotal=100
    redis.url=192.168.1.55
    redis.port=6379

    2 Utils部分

    1 JedisPoolUtils工具类

    package www.test.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()));//最大连接数
            pool = new JedisPool(poolConfig, pro.getProperty("redis.url"),
                    Integer.parseInt(pro.get("redis.port").toString()));
        }
    
        //获得jedis资源的方法
        public static Jedis getJedis() {
            return pool.getResource();
        }
    
    }

    3 web端 CategoryListServlet代码的修改

    package www.test.web.servlet;
    
    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.google.gson.Gson;
    
    import redis.clients.jedis.Jedis;
    import www.test.domain.Category;
    import www.test.service.ProductService;
    import www.test.utils.JedisPoolUtils;
    
    public class CategoryListServlet extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            ProductService service = new ProductService();
    
            //先从缓存中查询categoryList 如果有直接使用 没有再才从数据库中查询  查询获得之后存到缓存中
            //1、获得jedis对象 连接redis数据库
            Jedis jedis = JedisPoolUtils.getJedis();
            String categoryListJson = jedis.get("categoryListJson");
            
            //2、判断categoryListJson是否为空
            if(categoryListJson==null){
                System.out.println("缓存没有数据 查询数据库");
                
                // 准备分类数据
                List<Category> categoryList = null;
                try {
                    categoryList = service.findAllCategory();
                } catch (SQLException e) {
                    
                    e.printStackTrace();
                }
                //使用转换工具将categoryList转换成json格式
                Gson gson = new Gson();
                categoryListJson = gson.toJson(categoryList);
                //将从数据库中获得的categoryListJson存储到缓存中
                jedis.set("categoryListJson", categoryListJson);
            }
            
            //将转换后的json格式字符串写出
             //写出前先解决乱码问题
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(categoryListJson);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }

    其它代码和案例19一样的。

    上面修改可以达到优化的效果。从片存中获取数据的速度是最快的。

  • 相关阅读:
    管理者的四种不同授权风格
    centos7 未启用swap导致内存使用率过高。
    Dynamically create a div element with JavaScript/jQuery
    sql server: Parent/Child hierarchy tree view
    视频编解码系列(一)压缩编码基础常识
    mac 常用终端命令
    Linux下安装Python3.6.8
    SQL特殊字符转义
    Ehcache缓存监控
    指标管理体系设计
  • 原文地址:https://www.cnblogs.com/jepson6669/p/8444231.html
Copyright © 2020-2023  润新知