jedis高级使用

2024-08-27 16:18
文章标签 使用 高级 jedis

本文主要是介绍jedis高级使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

环境

jdk:1.7
jedis:2.6.0

事务

jedis为了使用事务,你需要把操作包裹在事务块中,这和管道非常类似:

jedis.watch (key1, key2, ...);
Transaction t = jedis.multi();
t.set("foo", "bar");
t.exec();

注意:当你有返回值的方法时,你需要这样做:

Transaction t = jedis.multi();
t.set("fool", "bar"); 
Response<String> result1 = t.get("fool");t.zadd("foo", 1, "barowitch");
t.zadd("foo", 0, "barinsky");
t.zadd("foo", 0, "barikoviev");
// get the entire sortedset
Response<Set<String>> sose = t.zrange("foo", 0, -1);
// dont forget it
t.exec();                                              
// use Response.get() to retrieve things from a Response
String foolbar = result1.get();      
// on sose.get() you can directly call Set methods!                 
int soseSize = sose.get().size();                      
// you could still get all results at once, as before
// List<Object> allResults = t.exec();                 

注意:

在执行t.exec()被执行之前response对象不包括结果。没有执行exec方法将会抛出异常redis.clients.jedis.exceptions.JedisDataException,在上面代码的最后一行,你将看到在版本2之前是如何处理事务/管道。你现在仍然可以使用这个方法,但是接着你需要从列表中提取对象,其中还包含redis状态信息。

注意2:

redis不允许在同一个事务中的使用中间结果。下面的代码不会工作

// this does not work! Intra-transaction dependencies are not supported by Redis!
jedis.watch(...);
Transaction t = jedis.multi();
if(t.get("key1").equals("something"))t.set("key2", "value2");
else t.set("key", "value");
However, there are some commands like setnx, that include such a conditional execution. Those are of course supported within transactions. You can build your own customized commands using eval/ LUA scripting.

Pipelining 管道

有时你需要发送一堆不同命令。使用管道,这么做会非常酷,这种方式会有非常好的性能。这种方式发送的命令不会进行等待响应,并且你会在最后读取到响应,这样非常快。

具体该怎么做呢?:

Pipeline p = jedis.pipelined();
p.set("fool", "bar"); 
p.zadd("foo", 1, "barowitch");  p.zadd("foo", 0, "barinsky"); p.zadd("foo", 0, "barikoviev");
Response<String> pipeString = p.get("fool");
Response<Set<String>> sose = p.zrange("foo", 0, -1);
p.sync(); int soseSize = sose.get().size();
Set<String> setBack = sose.get();

我做了下打印:

public static void pipelined(){JedisPool pool = new JedisPool(new JedisPoolConfig(), "192.168.116.19");try(Jedis jedis = pool.getResource()){jedis.flushDB();Pipeline p = jedis.pipelined();p.set("fool", "bar"); p.zadd("foo", 1, "barowitch");p.zadd("foo", 0, "barinsky");p.zadd("foo", 0, "barikoviev");Response<String> pipeString = p.get("fool");Response<Set<String>> sose = p.zrange("foo", 0, -1);p.sync();//这里一定要注意//该段代码不能再p.sync()方法之前,否则报错String pp = pipeString.get();System.out.println(pp);int soseSize = sose.get().size();System.out.println(soseSize);Set<String> setBack = sose.get();System.out.println(setBack);}}

注意:pipeString.get()这个是获取结果,所以肯定是先执行了才会有结果,即:必须要在p.sync()之后,否则报错:

Please close pipeline or multi block before calling this method.

更多的详情请查看 事务 章节部分中的代码注释。

Publish/Subscribe 发布/订阅

jedis中订阅一个频道,需要创建一个JedisPubSub并且在该jedis实例中调用subscribe方法:

class MyListener extends JedisPubSub {public void onMessage(String channel, String message) {}public void onSubscribe(String channel, int subscribedChannels) {}public void onUnsubscribe(String channel, int subscribedChannels) {}public void onPSubscribe(String pattern, int subscribedChannels) {}public void onPUnsubscribe(String pattern, int subscribedChannels) {}public void onPMessage(String pattern, String channel,String message) {}
}MyListener l = new MyListener();jedis.subscribe(l, "foo");

注意:订阅是一个阻塞操作,因为其会在线程中轮循jedis调用subscribe的响应。一个JedisPubSub实例可以被订阅多个频道。你可以在现有的JedisPubSub实例中调用subscribepsubscribe来改变你的订阅。

这篇关于jedis高级使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1112165

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML