当前位置:网站首页>Basic use of redis
Basic use of redis
2022-07-29 05:34:00 【Crying dogs in the sun】
package redis_learn;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class JedisDemo {
//@Test Indicates that this method is a unit test method
// Connect and add String Type data
@Test
public void fun1() {
// Direct connection redis database
Jedis jedis = new Jedis("127.0.0.1",6379);
// Set the connection password
// jedis.auth("123");
// add to String Type data
jedis.set("field1","i am field1");
// Output the added data ( According to the key , Output the corresponding value )
System.out.println(jedis.get("field1"));
// Delete String Type data ( Delete... According to the key )
// jedis.del("field1");
// Output data , See if the deletion succeeded
System.out.println(jedis.get("field1"));
}
// Connect and add hash Type data ( I understand it as giving String Types of data are classified , Every hash Can be stored 2^32-1 Key value pairs )
@Test
public void fun2(){
// Connect through connection pool redis database
Jedis jedis = RedisUtils.getJedis();
// add to hash Type data
jedis.hset("hset1","name"," Zhang San ");
jedis.hset("hset1","age","22");
jedis.hset("hset1","sex"," male ");
// get data
List<String> hmget = jedis.hmget("hset1", "name","age","sex");
// Output
System.out.println(hmget);
// Delete
jedis.hdel("hset1","name","sex");
// Delete before exporting , See if the deletion is successful
System.out.println(jedis.hmget("hset1", "name","age","sex"));
RedisUtils.returnResource(jedis);
}
// Connect and add List Type data ( queue , Sort by insertion order , You can add an element to the head of the list ( On the left ), Or tail ( On the right ))
@Test
public void fun3() {
// Connect through connection pool redis database
Jedis jedis = RedisUtils.getJedis();
// add to List Type data ,lpush Add to the list header , That is, the data added later is at the top
jedis.lpush("field2","aaa");
jedis.lpush("field2","bbb");
jedis.lpush("field2","ccc");
// From the index 1 Get the index from the location of 6 The value of the location , Because it exceeds the actual index 2, So we will continue to cycle output later
List<String> field2 = jedis.lrange("field2", 0, 6);
// Output the added key value pairs
for(String item:field2){
System.out.println(item);
}
RedisUtils.returnResource(jedis);
}
// Connect and add Set Type data ( A combination of non repeating values )
@Test
public void fun4(){
// Connect through connection pool redis database
Jedis jedis = RedisUtils.getJedis();
// add to Set Type data
jedis.sadd("name","zhangsan");
jedis.sadd("name","lisi");
jedis.sadd("name","lisi"); // Add the same data again , It will cover the last time
jedis.sadd("age","16");
jedis.sadd("sex","nan");
jedis.sadd("address","china");
// obtain Set Type data
Set<String> name = jedis.smembers("name");
// Output to get Set Type data ( The order of output is out of order )
System.out.println(name);
System.out.println("\n");
// Get current redis In the database , all key, With Set Return data in the way of collection
Set<String> set = jedis.keys("*");
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
RedisUtils.returnResource(jedis);
}
// Connect and add ZSet Type data (zset yes set Upgraded version , It's in set The order attribute is added on the basis of score, This attribute can be specified when adding modification elements , After each assignment ,zset Automatically reorder to the new value )
@Test
public void fun5(){
// Connect through connection pool redis database
Jedis jedis = RedisUtils.getJedis();
// add to zset Type data
jedis.zadd("field3",1,"hhh");
jedis.zadd("field3",0,"jjj");
jedis.zadd("field3",3,"bbb");
// obtain set Type data
Set<String> field3 = jedis.zrangeByScore("field3", 0, 5);
// Loop output with iterators
Iterator<String> iterator = field3.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
RedisUtils.returnResource(jedis);
}
}
边栏推荐
- Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
- Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?
- 三次握手四次挥手针对面试总结
- 公众号不支持markdown格式文件编写怎么办?
- 牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
- ANSI C类型限定符
- Occt learning 001 - Introduction
- 【C语言系列】—三种方法模拟实现strlen库函数的方法
- 365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
- Camunda 1. Camunda workflow - Introduction
猜你喜欢
C语言 一级指针
eggjs 创建应用知识点
About local variables
Day 3
无重复字符的最长字串
【C语言系列】— 把同学弄糊涂的 “常量” 与 “变量”
Day 1
365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
Realize simple database query (incomplete)
随机推荐
副作用和序列点
Talking about Servlet
一维数组练习
哈夫曼树以及哈夫曼编码在文件压缩上的应用
重定向和文件
【C语言系列】—深度解剖数据在内存中的存储(二)-浮点型
Occt learning 003 - MFC single document project
D3d Shader Instruction
Clickhouse learning (VII) table query optimization
Dynamic sorting of DOM object element blocks in applets
牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍
ClickHouse学习(八)物化视图
小程序中的DOM对象元素块动态排序
paddle. Fluid constant calculation error 'nonetype' object has no attribute 'get_ fetch_ list‘
ClickHouse学习(三)表引擎
Day 3
抽象类与接口
js深拷贝-笔记
数组学习之入门简单题 两数之和