Redis-使用Java代码操作Redis

4 篇文章 0 订阅

1、redis的基本Java操作

准备工作

将VMware Workstation Pro登录启动后就设置在后台运行
然后打开redis数据库

在这里插入图片描述

jedis连接
 		Jedis jedis = new Jedis("192.168.198.128",6379);//指定Redis服务Host和port
        jedis.auth("123456");//连接密码
        System.out.println(jedis.ping());

在这里插入图片描述
连接成功了

操作字符串
		 jedis.set("aa","zs");
        System.out.println(jedis.get("aa"));
操作哈希

存储哈希

		jedis.hset("user1","name","阿豆");
        jedis.hset("user1","sex","男");

在这里插入图片描述
取哈希

 System.out.println(jedis.hgetAll("user1"));//取所有
        System.out.println(jedis.hget("user1", "name"));//取名字

在这里插入图片描述

操作列表list
		jedis.lpush("hobby","a","b","c","d","e");
        System.out.println(jedis.lpop("hobby"));
        System.out.println(jedis.rpop("hobby"));

先进后出
在这里插入图片描述

2、redis应用到查询中

导入依赖servlet

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

首页home.jsp

<%--
  Created by IntelliJ IDEA.
  User: zrh
  Date: 2019/9/19
  Time: 20:52
  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>
</head>
<body>
博客首页
拿取数据的方式:${msg}<br>
拿去到的数:${currentUser}

</body>
</html>

DemoServlet

package com.zrh;

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 zrh
 * @site IDEA项目
 * @company
 * @create 2019-09-1920:51
 */
@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.198.128",6379);//指定Redis服务Host和port
        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 name = "zhangsan";
            String pass = "123456";
//            接下来把数据库中的对象存储到缓存中去
            jedis.hset("currentUser","name","zhangsan");
            jedis.hset("currentUser","pass","123456");
//            此时能获取到值原因是上面已经将数据存储到缓存中
            currentUser = jedis.hgetAll("currentUser");
            req.setAttribute("currentUser",currentUser);
        }
        req.getRequestDispatcher("home.jsp").forward(req,resp);
    }
}

界面展示
在这里插入图片描述
注意Idea下JDK版本和Tomcat版本冲突:
报错信息

19-Sep-2019 20:17:06.984 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method createStandardContext
javax.management.RuntimeOperationsException: Exception invoking method manageApp

解决方案:
jdk:1.8
Tomcat:tomcat-9.0.14
将Tomacat版本退到:tomcat-8.0.23问题解决;
tomcat-8.0.23压缩包百度云资源:tomcat-8.0的地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis 是一个开源的内存数据存储系统,提供了丰富的数据结构和功能,被广泛用于缓存、消息队列、实时统计等场景。为了与 Redis 进行交互,可以使用各种编程语言提供的 Redis 客户端库。 Redis 客户端库允许开发者通过编程语言来与 Redis 服务器进行通信,并执行各种操作,例如设置和获取键值对、执行批量操作、发布和订阅消息等。 不同的编程语言提供了不同的 Redis 客户端库,以下是一些常用的编程语言与对应的 Redis 客户端库: - Python: `redis-py` (https://github.com/andymccurdy/redis-py) - Java: Jedis (https://github.com/xetorthio/jedis) - Node.js: `ioredis` (https://github.com/luin/ioredis) - Ruby: `redis-rb` (https://github.com/redis/redis-rb) - Go: `go-redis` (https://github.com/go-redis/redis) 使用这些客户端库,您可以连接到 Redis 服务器,并执行各种操作。例如,在 Python 中使用 `redis-py` 客户端库连接到 Redis 服务器并设置一个键值对,可以执行以下代码: ```python import redis # 创建 Redis 客户端 r = redis.Redis(host='localhost', port=6379, db=0) # 设置键值对 r.set('key', 'value') # 获取键值对 value = r.get('key') print(value) ``` 上述代码创建了一个 Redis 客户端对象 `r`,连接到本地的 Redis 服务器(默认端口为 6379),并设置了一个键值对 `key: value`。然后,通过 `get` 方法获取键 `key` 的值,并打印出来。 根据您使用的编程语言和相应的 Redis 客户端库,您可以以类似的方式与 Redis 进行交互。请参考相应的文档和示例代码以了解更多细节和操作

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值