Jedis简介
实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis,
对于主流语言,Redis都提供了对应的客户端;
提供了很多客户端 官方推荐的是Jedis 托管地址:https://github.com/xetorthio/jedis
1. Java访问redis
1 添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
1.2 Java连接redis
Jedis jedis = new Jedis(ip, port);
jedis.auth("123456");//权限认证
jedis.ping();
jedis.select(0);//切换数据库
1.3 Java操作redis
string(字符串)
hash(哈希)
list(列表)
set(集合)
zset(sorted set:有序集合)
zadd/zrevrange
注1:不需要记得API的方法,只需要查redis命令
使用Jedis连接池之后,在每次用完连接对象后一定要记得把连接归还给连接池。Jedis对close方法进行了改造,如果是连接池中的连接对象,调用Close方法将会是把连接对象返回到对象池,若不是则关闭连接。
package com.houyitao;
import redis.clients.jedis.Jedis;
/**
* @authorHYT
* @site www.hyt.com
* @company 阿里巴巴
* @create 2019-10-13 20:36
*/
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("name","wupeng");
System.out.println(jedis.get("name"));
//对哈希进行操作
jedis.hset("user1","name","tugou");
jedis.hset("user1","sex","nv");
jedis.hset("user1","age","18");
System.out.println(jedis.hgetAll("user1"));
System.out.println(jedis.hget("user1", "name"));
//对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 com.houyitao;
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;
/**
* @authorHYT
* @site www.hyt.com
* @company 阿里巴巴
* @create 2019-10-13 23:50
*/
@WebServlet("/houyitao")
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);
}
}
}
jsp
<html>
<body>
<h2>Hello World!</h2>
<%--
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>