java代码操作redis
添加redis的pom依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
连接数据库:
Jedis jedis = new Jedis(ip, port);//主机IP,port是端口
jedis.auth("123456");//密码
jedis.ping();//测试是否成功 返回pong则成功
jedis.select(0);//切换数据库
基础操作:
Dame1
package com.Tang;
import redis.clients.jedis.Jedis;
/**
* @author Tang
*/
//连接redis
public class Dame1 {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.19.128",6379);
jedis.auth("123456");
System.out.println(jedis.ping());
// 操作字符串
// jedis.set("aaa","zs");
// System.out.println(jedis.get("aaa"));
// 操作哈希
// jedis.hset("user","uname","ls");
// jedis.hset("user","sex","ww");
// System.out.println(jedis.hgetAll("user"));
// System.out.println(jedis.hget("user", "uname"));
// 操作列表
jedis.lpush("hobby","a","b","c","d","e");
System.out.println(jedis.lpop("hobby"));
System.out.println(jedis.lpop("hobby"));
System.out.println(jedis.rpop("hobby"));
}
}
测试:
DemoServlet
package com.Tang;
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;
import java.util.Map;
/**
* @author Tang
*/
@WebServlet("/getData")
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.19.128",6379);
jedis.auth("123456");
// 从缓存中获取当前登录的用户信息
Map<String, String> currentUser = jedis.hgetAll("currentUser");
if(currentUser !=null && currentUser.size()>0){
req.setAttribute("msg","从缓存中获取数据");
req.setAttribute("currentUser",currentUser);
}else{
// 第一次登录,第一次访问首页数据
req.setAttribute("msg","从数据库中获取数据");
String uname = "tang";
String upass = "123";
// 接下来把数据库的对象存储到缓存中
jedis.hset("currentUser","uname","tang");
jedis.hset("currentUser","upass","123");
// 已经放入缓存中
currentUser = jedis.hgetAll("currentUser");
req.setAttribute("currentUser",currentUser);
}
req.getRequestDispatcher("home.jsp").forward(req,resp);
}
}
home.jsp
<%--
Created by IntelliJ IDEA.
User: Tang
Date: 2019/9/19
Time: 22:21
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}<br>
拿取到的数据:${currentUser}
</body>
</html>
第一次访问(数据库取值)
第二次访问(缓存取值)
运用:
1、首页第一次是读取数据库,后面读取缓存(在没有增删改的情况)
//获取redis中的数据
String blogListJsonStr = jedis.get("blogList");
//如果redis中有值
if(blogListJsonStr!= null && blogListJsonStr.length() > 0) {
// 使用redis
request.setAttribute("blogList", JSON.parse(blogListJsonStr));
}else {
//使用数据库
//从数据库中查询数据
List<Map<String, Object>> blogList = this.blogDao.freemarker_list(title, null);
//放进缓存
jedis.set("blogList", JSON.toJSONString(blogList));
//存作用域
request.setAttribute("blogList", blogList);
}
2、增删改的时候,要顺带更新缓存,下一次再次访问首页要保证redis中数据跟mysql数据是一致:
public String add() {
HttpServletRequest request = ServletActionContext.getRequest();
Map<String,String[]> parameterMap = request.getParameterMap();
try {
//清空缓存
jedis.del("blogList");
//加入数据库
this.blogDao.save(parameterMap);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}
public String del() {
try {
HttpServletRequest request = ServletActionContext.getRequest();
//清空缓存
jedis.del("blogList");
// 数据库中删除博客
this.blogDao.del(request.getParameterMap());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}