使用java程式碼操作redis

Sinner°發表於2020-10-15

Java訪問redis

string(字串)
hash(雜湊)
list(列表)
set(集合)
zset(sorted set:有序集合)
zadd/zrevrange

新增依賴

      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
      </dependency>

注1:不需要記得API的方法,只需要查redis命令

Demo.jsp

package com.leiliang.redis;

import redis.clients.jedis.Jedis;
import sun.applet.Main;

import java.util.Map;

/**
 * @authorleiliang
 * @site www.leiliang.com
 * @company xxx公司
 * @create  2020-10-08 10:14
 *
 *
 * 講解Java程式碼操作redis
 * string 、hash 、list
 *
 *
 * 1、載入驅動
 * 2、建立連線(url、uname、pwd)
 * 3、preparestatement
 * 4、執行sql
 * 5、處理結果
 * 6、關閉資源
 *
 *
 * redis連線步驟
 * 1、建立連線、連線後授權
 * 2、使用redis
 */
public class Demo1 {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.222.129",6379);
        jedis.auth("123456");

        System.out.println(jedis.ping());//代表連線成功

        /**
         *  String
         */
//        jedis.set("name","zs");
//        jedis.set("age","22");
//        System.out.println(jedis.get("name"));

        /**
         * hash
         */
        //存
//        jedis.hset("user1","uname","ls");
//        jedis.hset("user1","pwd","123");
//        jedis.hset("user1","sex","nan");
        //取
//        System.out.println(jedis.hget("user1", "uname"));
//        Map<String, String> user1 = jedis.hgetAll("user1");
//        for (Map.Entry<String, String> entry : user1.entrySet()) {
//            System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
//        }

        /**
         * list
         */
//        jedis.lpush("hobby","a","b","c","d","e","f","g");
//        System.out.println(jedis.lpop("hobby"));//從後面彈出
//        System.out.println(jedis.rpop("hobby"));


    }
}


DemoServlet

1.新增servlet的依賴

 <javax.servlet.version>4.0.0</javax.servlet.version>
 
<dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>${javax.servlet.version}</version>
          <scope>provided</scope>
 </dependency>
package com.leiliang.redis;

import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @authorleiliang
 * @site www.leiliang.com
 * @company xxx公司
 * @create  2020-10-08 17:03
 *
 * redis  在專案中的使用
 *    查詢
 *    增刪改
 */
@WebServlet("/list")
public class DemoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Jedis jedis = new Jedis("192.168.222.129",6379);
        jedis.auth("123456");

        String bookList = jedis.get("bookList");
        if(null == bookList || "".equals(bookList)){
//          查詢資料庫
            String mysqlDate="data";
//            將mysqlDate轉成json陣列串
            jedis.set("bookList",mysqlDate);
            bookList=  jedis.get("bookList");
            req.setAttribute("msg","走了資料庫拿資料");
            req.setAttribute("bookList",bookList);
            req.getRequestDispatcher("/bookList.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg","直接從redis中取得資料");
            req.setAttribute("bookList",bookList);
            req.getRequestDispatcher("/bookList.jsp").forward(req,resp);
        }
    }
}

bookList.jsp

<%--
  Created by IntelliJ IDEA.
  User: zjjt
  Date: 2020/10/10
  Time: 19:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
 ${msg};${bookList};
</body>
</html>

可以開啟F12來看redis的速度,這裡只是死資料模擬
1.第一次訪問資料庫

2.第二次訪問,直接從redis快取中獲取

 

 

 

 

 

 

 

 

相關文章