当前位置:网站首页>Redis - 5、Jedis操作Redis6
Redis - 5、Jedis操作Redis6
2022-06-22 11:28:00 【Q.E.D.】
1、介绍
Jedis是java开发的操作redis的工具包。
2、Jedis的用法
2.1、引入maven依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.1</version>
</dependency>
2.2、使用redis的api操作redis
案例代码如下,重点在于 Jedis 工具类,这个类中包含了操作redis的所有方法。
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @className JedisDemo
* @date 2022/6/21
**/
public class JedisDemo {
Jedis jedis;
@Before
public void before() {
this.jedis = new Jedis("127.0.0.1", 6379);
}
@After
public void after() {
//关闭jedis
this.jedis.close();
}
/**
* 测试redis是否连通
*/
@Test
public void test1() {
String ping = jedis.ping();
System.out.println(ping);
}
/**
* string类型测试
*/
@Test
public void stringTest() {
jedis.set("site", "http://www.itsoku.com");
System.out.println(jedis.get("site"));
System.out.println(jedis.ttl("site"));
}
/**
* list类型测试
*/
@Test
public void listTest() {
jedis.rpush("courses", "java", "spring", "springmvc", "springboot");
List<String> courses = jedis.lrange("courses", 0, -1);
for (String course : courses) {
System.out.println(course);
}
}
/**
* set类型测试
*/
@Test
public void setTest() {
jedis.sadd("users", "tom", "jack", "ready");
Set<String> users = jedis.smembers("users");
for (String user : users) {
System.out.println(user);
}
}
/**
* hash类型测试
*/
@Test
public void hashTest() {
jedis.hset("user:1001", "id", "1001");
jedis.hset("user:1001", "name", "张三");
jedis.hset("user:1001", "age", "30");
Map<String, String> userMap = jedis.hgetAll("user:1001");
System.out.println(userMap);
}
/**
* zset类型测试
*/
@Test
public void zsetTest() {
jedis.zadd("languages", 100d, "java");
jedis.zadd("languages", 95d, "c");
jedis.zadd("languages", 70d, "php");
List<String> languages = jedis.zrange("languages", 0, -1);
System.out.println(languages);
}
/**
* 订阅消息
*
* @throws InterruptedException
*/
@Test
public void subscribeTest() throws InterruptedException {
// subscribe(消息监听器,频道列表)
jedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
System.out.println(channel + ":" + message);
}
}, "sitemsg");
TimeUnit.HOURS.sleep(1);
}
/**
* 发布消息
*
* @throws InterruptedException
*/
@Test
public void publishTest() {
jedis.publish("sitemsg", "hello redis");
}
}边栏推荐
- Electron adding SQLite database
- puzzle(019)平面正推问题
- 分治思想在海量数据处理中的应用
- The R language dplyr package mutate function divides two numeric variables in the dataframe to create a new data column (create a new variable)
- CF751 C. Optimal Insertion
- 美团基于 Flink 的实时数仓平台建设新进展
- Wechat applet project example - image processing gadget (self-made low configuration version of Meitu XiuXiu)
- lyndon分解学习笔记
- 奋斗吧,程序员——第四十二章 会挽雕弓如满月,西北望,射天狼
- Reader case of IO
猜你喜欢

把蔚来告上法庭,奥迪急什么?

【软工】 概论 & 过程和生命周期建模

xlrd. biffh. XLRDError: Excel xlsx file; Not supported solution

PHP database mysql question

Electron ajoute une base de données SQLite

Solution to 55e of Niuke challenge

什么是同源???跨域错误???如何解决???
![两两交换链表中的节点[单向链表不断链原则]](/img/67/8e9f3c396a8f529a616964b69cc47f.png)
两两交换链表中的节点[单向链表不断链原则]

What is homology??? Cross domain error??? How to solve???

牛客挑战赛55E题解
随机推荐
CF751 C. Optimal Insertion
俞敏洪称未来可能开电商学院;马斯克儿子申请断绝父子关系;饿了么回应大量用户收到免单信息;B站上线付费视频...
有时候努力,并不是自己心甘情愿
PHP database mysql question
奋斗吧,程序员——第四十五章 柔情似水,佳期如梦
牛客挑战赛54E题解
MATLAB中cellstr函数的使用
SAP Marketing Cloud 功能概述(二)
“不敢去怀疑代码,又不得不怀疑代码”记一次网络请求超时分析
CF edu118f problem solving
国外LEAD需要干劲、兴趣、钻研、勤奋、缺一不可
Cookies and sessions for answers to common interview questions
Call center CTI system
What is the image used to parse the Tso of the DN binlog? It seems that there is no direct use of mysqlbinlog?
R language uses GLM function to build Poisson log linear regression model, processes three-dimensional contingency table data to build saturation model, and uses summary function to obtain model summa
Bytestream case of IO
CF751E Phys Ed Online
[2022 summer] [leetcode] 31 Next spread
[CISCN2019 总决赛 Day1 Web4]Laravel1
APM flight mode switching -- source code explanation