您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用Java怎么操作Redis,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
連接redis
Jedis jedis = new Jedis("192.168.234.131",6379); jedis.auth("123456"); //jedis.close(); //使用完關閉連 System.out.println(jedis.ping());
使用Jedis連接池之后,在每次用完連接對象后一定要記得把連接歸還給連接池。Jedis對close方法進行了改造,如果是連接池中的連接對象,調用Close方法將會是把連接對象返回到對象池,若不是則關閉連接。
操作Redis常用的三種數據類型 ,還有兩種不常用的就不介紹了
package com.wp; import redis.clients.jedis.Jedis; /** * @author 小李飛刀 * @site www.xiaomage.com * @company zhuojing * @create 2019-10-13 10:43 */ public class javaDome { public static void main(String[] args) { //鏈接操作 Jedis jedis = new Jedis("192.168.234.131",6379); jedis.auth("123456"); System.out.println(jedis.ping()); //操作字符串 jedis.set("aaa","張三"); System.out.println(jedis.get("aaa")); //對哈希進行操作 jedis.hset("user1","uname","李四"); jedis.hset("user1","sex","男"); System.out.println(jedis.hgetAll("user1")); System.out.println(jedis.hget("user1", "uname")); //對list進行操作 jedis.lpush("hobby","a","b","c","d","e","f"); System.out.println(jedis.lpop("hobby")); System.out.println(jedis.lpop("hobby")); System.out.println(jedis.rpop("hobby")); } }
項目中的應用演示例子
查詢中使用redis的邏輯
如圖所示:
bookservlet.java
代碼如下:
package web; 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; /** * @author 小李飛刀 * @site www.xiaomage.com * @company zhuojing * @create 2019-10-13 10:43 */ @WebServlet("/wp") public class Bookservlet 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.234.131",6379); jedis.auth("123456"); String booklist = jedis.get("booklist"); if(booklist==null || "".equals(booklist)){ //模擬實際項目開發需求,在項目中運用redis //查詢數據庫 String mysqldata="data"; //將mysqldata數據源轉成json數組串 jedis.set("booklist",mysqldata); booklist = jedis.get("booklist"); req.setAttribute("mag","走了數據庫數據"); req.setAttribute("booklist",booklist); req.getRequestDispatcher("/booklist.jsp").forward(req,resp); } else{ req.setAttribute("mag","直接從redis里面拿了數據"); req.setAttribute("booklist",booklist); req.getRequestDispatcher("/index.jsp").forward(req,resp); } } }
index.jsp
<html> <body> <h3>Hello World!</h3> <%-- Created by IntelliJ IDEA. User: machenike Date: 2019/10/13 Time: 11:53 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> ${mag}:${booklist} </body> </html>
上述內容就是使用Java怎么操作Redis,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。