1. 程式人生 > >redis在專案中的使用

redis在專案中的使用

1.匯入jedis和池子的jar包

https://mvnrepository.com/artifact/org.apache.commons/commons-pool2/2.3

https://mvnrepository.com/artifact/redis.clients/jedis/2.7.0

2.匯入工具類

package com.itheima.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();
    }
    
    public static void main(String[] args) {
        Jedis jedis = getJedis();
        System.out.println(jedis.get("xxx"));
    }
}

 3.匯入配置檔案

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

4. 編寫程式碼

package com.itheima.web.servlet;

import java.io.IOException;
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 com.itheima.domain.Category;
import com.itheima.service.ProductService;
import com.itheima.utils.JedisPoolUtils;

import redis.clients.jedis.Jedis;

public class CategoryListServlet extends HttpServlet {

    protected 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 = service.findAllCategory();
            Gson gson = new Gson();
            categoryListJson = gson.toJson(categoryList);
            jedis.set("categoryListJson", categoryListJson);
        }
        
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(categoryListJson);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}