使用java代码操作redis

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缓存中获取

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值