基于Java代码操作Redis过程详解
作者:江东maker 时间:2022-03-28 06:53:58
Jedis简介
实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis,
对于主流语言,Redis都提供了对应的客户端;
提供了很多客户端 官方推荐的是Jedis 托管地址:https://github.com/xetorthio/jedis
要使用redis首先得下载pom依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
连接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>
<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>
来源:https://www.cnblogs.com/wupeng/p/11669130.html
标签:java,代码,操作,redis
0
投稿
猜你喜欢
Spring Boot 防止接口恶意刷新和暴力请求的实现
2022-05-08 03:54:58
通过LinQ查询字符出现次数的实例方法
2023-12-10 13:40:01
Spring Security认证提供程序示例详解
2022-11-07 18:06:40
Spring Boot ActiveMQ连接池配置过程解析
2023-11-08 23:08:02
Java面试题冲刺第十六天--消息队列
2022-08-08 09:07:04
Java中Set集合的使用和底层原理解析
2022-12-16 20:54:28
Spring Data JPA中 in 条件参数的传递方式
2023-11-08 06:45:47
maven引入本地jar包运行报错java.lang.NoClassDefFoundError解决
2022-10-15 01:42:24
Java发送报文与接收报文的实例代码
2023-07-09 04:44:01
Java Socket编程实例(四)- NIO TCP实践
2021-10-07 13:44:35
java map中相同的key保存多个value值方式
2022-12-12 20:05:45
Android实现圆角Button按钮
2022-02-04 10:05:45
C# 实现颜色的梯度渐变案例
2023-11-20 22:01:06
springBoot集成Elasticsearch 报错 Health check failed的解决
2022-12-07 05:18:16
如何在MyBatis中实现DataSource
2021-11-29 13:49:48
Java经典用法总结(二)
2023-11-24 20:39:10
Java 数据结构与算法系列精讲之贪心算法
2023-09-12 17:56:13
Java实现生成n个不重复的随机数
2021-09-16 04:47:07
C#获取CPU编号的方法
2022-01-05 10:57:37
Android Q适配之IMEI替换为Android_id
2022-08-08 00:46:01