当前位置:网站首页>Redis - 5. Jedis operation redis6
Redis - 5. Jedis operation redis6
2022-06-22 12:01:00 【Q.E.D.】
1、 Introduce
Jedis yes java Developed operations redis The toolkit .
2、Jedis Usage of
2.1、 introduce maven rely on
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.1</version>
</dependency>
2.2、 Use redis Of api operation redis
The case code is as follows , The point is Jedis Tool class , This class contains operations redis All the ways .
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() {
// close jedis
this.jedis.close();
}
/**
* test redis Is it connected
*/
@Test
public void test1() {
String ping = jedis.ping();
System.out.println(ping);
}
/**
* string Type testing
*/
@Test
public void stringTest() {
jedis.set("site", "http://www.itsoku.com");
System.out.println(jedis.get("site"));
System.out.println(jedis.ttl("site"));
}
/**
* list Type testing
*/
@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 Type testing
*/
@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 Type testing
*/
@Test
public void hashTest() {
jedis.hset("user:1001", "id", "1001");
jedis.hset("user:1001", "name", " Zhang San ");
jedis.hset("user:1001", "age", "30");
Map<String, String> userMap = jedis.hgetAll("user:1001");
System.out.println(userMap);
}
/**
* zset Type testing
*/
@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);
}
/**
* Subscribe to news
*
* @throws InterruptedException
*/
@Test
public void subscribeTest() throws InterruptedException {
// subscribe( Message listener , Channel list )
jedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
System.out.println(channel + ":" + message);
}
}, "sitemsg");
TimeUnit.HOURS.sleep(1);
}
/**
* Release the news
*
* @throws InterruptedException
*/
@Test
public void publishTest() {
jedis.publish("sitemsg", "hello redis");
}
}边栏推荐
- Redis - 10、主从复制
- 奋斗吧,程序员——第三十九章 人生不失意,焉能慕知己
- Wechat applet project example - image processing gadget (self-made low configuration version of Meitu XiuXiu)
- [CISCN2019 总决赛 Day1 Web4]Laravel1
- Redis - 8、持久化之RDB(Redis DataBase)
- 关于缓存异常:缓存雪崩、击穿、穿透的解决方案
- Call center CTI system
- Struggle, programmer -- Chapter 36 the falling flower man is independent and the tiny swift flies
- Software architecture design principles
- Interpretation of basic requirements for classified protection of network security (GBT 22239-2019)
猜你喜欢

关于缓存异常:缓存雪崩、击穿、穿透的解决方案

【软工】计划和项目管理

Redis - 11. Cluster

《梦华录》成吸金王:广告主投500万排不上队,腾讯视频赢麻了?

Wechat applet project example - image processing gadget (self-made low configuration version of Meitu XiuXiu)
![[2206] An Improved One millisecond Mobile Backbone](/img/75/b040f4b88050937dee57003b62f7b0.png)
[2206] An Improved One millisecond Mobile Backbone

Development technology of NFT trading platform digital collection system

在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式

Redis - 7. Transaction operation

Redis - 9. Persistent AOF (append onlyfile)
随机推荐
Solution to 57c of Niuke challenge
牛客练习赛94F题解
Solution to 54e of Niuke challenge
CF751 C. Optimal Insertion
"Dare not doubt the code, but have to doubt the code" a network request timeout analysis
SPI 与 API的区别
奋斗吧,程序员——第五十章 海内存知己,天涯若比邻
haas506 2.0开发教程-高级组件库-modem.info(仅支持2.2以上版本)
NOI使用案例
IO之Reader案例
Lyndon breaks down learning notes
牛客挑战赛54E题解
The use of cellstr function in MATLAB
牛客挑战赛54F题解 & 李超树学习笔记
[WMCTF2020]Make PHP Great Again 2.0
Redis - 9、持久化之AOF(Append OnlyFile)
"Meng Hua Lu" has become the king of gold: advertisers can't line up with 5million yuan. Has Tencent video won?
Redis - 5、Jedis操作Redis6
promise升级版async,await来袭,搭配try+catch更香哦
奋斗吧,程序员——第四十八章 千金纵买相如赋,脉脉此情谁诉