当前位置:网站首页>Redis uses jedis operation
Redis uses jedis operation
2022-07-22 15:12:00 【Just number six Z】
Redis Use Jedis operation
Jedis
We're going to use java To operate redis
jedis yes Redis Officially recommended java Connect development tools , Use java operation redis middleware
1. newly build maven Project connection test
(1) Import jedis rely on
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
(2) Coding test
public class TestPing {
public static void main(String[] args) {
//1.new Jedis object
Jedis jedis=new Jedis("127.0.0.1",6379);
//2.jedis All the commands we have learned before redis command
System.out.println(jedis.ping());
}
}
Control output :
PONG
2. frequently-used API
testKey
public class TestKey {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
System.out.println(" Empty data :"+jedis.flushDB());
System.out.println(" Determine whether a key exists :"+jedis.exists("username"));
System.out.println(" newly added <'username','kuangshen'> The key/value pair :"+jedis.set("username", "kuangshen"));
System.out.println(" newly added <'password','password'> The key/value pair :"+jedis.set("password", "password"));
System.out.print(" All keys in the system are as follows :");
Set<String> keys = jedis.keys("*");
System.out.println(keys);
System.out.println(" Delete key password:"+jedis.del("password"));
System.out.println(" Judgment key password Whether there is :"+jedis.exists("password"));
System.out.println(" View key username The type of value stored :"+jedis.type("username"));
System.out.println(" Random return key One of the spaces :"+jedis.randomKey());
System.out.println(" rename key:"+jedis.rename("username","name"));
System.out.println(" Take out the modified name:"+jedis.get("name"));
System.out.println(" Search by index :"+jedis.select(0));
System.out.println(" Delete all... In the currently selected database key:"+jedis.flushDB());
System.out.println(" Return to the current database key Number of :"+jedis.dbSize());
System.out.println(" Delete all in all databases key:"+jedis.flushAll());
}
}
testString
public class TestString {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("=========== Add data ===========");
System.out.println(jedis.set("key1","value1"));
System.out.println(jedis.set("key2","value2"));
System.out.println(jedis.set("key3", "value3"));
System.out.println(" Delete key key2:"+jedis.del("key2"));
System.out.println(" Get key key2:"+jedis.get("key2"));
System.out.println(" modify key1:"+jedis.set("key1", "value1Changed"));
System.out.println(" obtain key1 Value :"+jedis.get("key1"));
System.out.println(" stay key3 Then add the value :"+jedis.append("key3", "End"));
System.out.println("key3 Value :"+jedis.get("key3"));
System.out.println(" Add multiple key value pairs :"+jedis.mset("key01","value01","key02","value02","key03","value03"));
System.out.println(" Get multiple key value pairs :"+jedis.mget("key01","key02","key03"));
System.out.println(" Get multiple key value pairs :"+jedis.mget("key01","key02","key03","key04"));
System.out.println(" Delete multiple key value pairs :"+jedis.del("key01","key02"));
System.out.println(" Get multiple key value pairs :"+jedis.mget("key01","key02","key03"));
jedis.flushDB();
System.out.println("=========== The new key value pair prevents the original value from being overridden ==============");
System.out.println(jedis.setnx("key1", "value1"));
System.out.println(jedis.setnx("key2", "value2"));
System.out.println(jedis.setnx("key2", "value2-new"));
System.out.println(jedis.get("key1"));
System.out.println(jedis.get("key2"));
System.out.println("=========== Add a key value pair and set the effective time =============");
System.out.println(jedis.setex("key3", 2, "value3"));
System.out.println(jedis.get("key3"));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(jedis.get("key3"));
System.out.println("=========== To obtain original value , Update to the new value ==========");
System.out.println(jedis.getSet("key2", "key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println(" get key2 The string of the value of :"+jedis.getrange("key2", 2, 4));
}
}
testSet
public class TestSet {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("============ Add elements to the collection ( No repetition )============");
System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println("eleSet All elements of are :"+jedis.smembers("eleSet"));
System.out.println(" Delete an element e0:"+jedis.srem("eleSet", "e0"));
System.out.println("eleSet All elements of are :"+jedis.smembers("eleSet"));
System.out.println(" Delete two elements e7 and e6:"+jedis.srem("eleSet", "e7","e6"));
System.out.println("eleSet All elements of are :"+jedis.smembers("eleSet"));
System.out.println(" Randomly remove an element from the set :"+jedis.spop("eleSet"));
System.out.println(" Randomly remove an element from the set :"+jedis.spop("eleSet"));
System.out.println("eleSet All elements of are :"+jedis.smembers("eleSet"));
System.out.println("eleSet The number of elements contained in :"+jedis.scard("eleSet"));
System.out.println("e3 Whether in eleSet in :"+jedis.sismember("eleSet", "e3"));
System.out.println("e1 Whether in eleSet in :"+jedis.sismember("eleSet", "e1"));
System.out.println("e1 Whether in eleSet in :"+jedis.sismember("eleSet", "e5"));
System.out.println("=================================");
System.out.println(jedis.sadd("eleSet1", "e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println(jedis.sadd("eleSet2", "e1","e2","e4","e3","e0","e8"));
System.out.println(" take eleSet1 Delete in e1 And deposit in eleSet3 in :"+jedis.smove("eleSet1", "eleSet3", "e1"));// Move to the collection element
System.out.println(" take eleSet1 Delete in e2 And deposit in eleSet3 in :"+jedis.smove("eleSet1", "eleSet3", "e2"));
System.out.println("eleSet1 The elements in :"+jedis.smembers("eleSet1"));
System.out.println("eleSet3 The elements in :"+jedis.smembers("eleSet3"));
System.out.println("============ Set operations =================");
System.out.println("eleSet1 The elements in :"+jedis.smembers("eleSet1"));
System.out.println("eleSet2 The elements in :"+jedis.smembers("eleSet2"));
System.out.println("eleSet1 and eleSet2 Intersection :"+jedis.sinter("eleSet1","eleSet2"));
System.out.println("eleSet1 and eleSet2 Union :"+jedis.sunion("eleSet1","eleSet2"));
System.out.println("eleSet1 and eleSet2 The difference between the set :"+jedis.sdiff("eleSet1","eleSet2"));//eleSet1 There is ,eleSet2 There is no
jedis.sinterstore("eleSet4","eleSet1","eleSet2");// Find the intersection and save it to dstkey Set
System.out.println("eleSet4 The elements in :"+jedis.smembers("eleSet4"));
}
}
testList
public class TestList {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("=========== Add one list===========");
jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
jedis.lpush("collections", "HashSet");
jedis.lpush("collections", "TreeSet");
jedis.lpush("collections", "TreeMap");
System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));//-1 For the last element ,-2 Represents the penultimate element ,end by -1 Means to query all
System.out.println("collections Section 0-3 The elements of :"+jedis.lrange("collections",0,3));
System.out.println("===============================");
// Delete the value specified in the list , The second parameter is the number of deletions ( When there are repetitions ), after add The value entered is deleted first , It's like getting out of the stack
System.out.println(" Delete the specified number of elements :"+jedis.lrem("collections", 2, "HashMap"));
System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
System.out.println(" Delete the following table 0-3 Elements outside the interval :"+jedis.ltrim("collections", 0, 3));
System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
System.out.println("collections List out ( Left side ):"+jedis.lpop("collections"));
System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
System.out.println("collections Additive elements , From the right end of the list , And lpush Corresponding :"+jedis.rpush("collections", "EnumMap"));
System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
System.out.println("collections List out ( Right end ):"+jedis.rpop("collections"));
System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
System.out.println(" modify collections Specify subscript 1 The content of :"+jedis.lset("collections", 1, "LinkedArrayList"));
System.out.println("collections The content of :"+jedis.lrange("collections", 0, -1));
System.out.println("===============================");
System.out.println("collections The length of :"+jedis.llen("collections"));
System.out.println(" obtain collections Subscript to be 2 The elements of :"+jedis.lindex("collections", 2));
System.out.println("===============================");
jedis.lpush("sortedList", "3","6","2","0","7","4");
System.out.println("sortedList Before ordering :"+jedis.lrange("sortedList", 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList After ordering :"+jedis.lrange("sortedList", 0, -1));
}
}
testHash
public class TestHash {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
Map<String,String> map = new HashMap<String,String>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
// Add the name hash(key) Of hash Elements
jedis.hmset("hash",map);
// The name of Xiang is hash Of hash Add key by key5,value by value5 Elements
jedis.hset("hash", "key5", "value5");
System.out.println(" hash hash All key value pairs of are :"+jedis.hgetAll("hash"));//return Map<String,String>
System.out.println(" hash hash All the keys of are :"+jedis.hkeys("hash"));//return Set<String>
System.out.println(" hash hash All values of are :"+jedis.hvals("hash"));//return List<String>
System.out.println(" take key6 The saved value plus an integer , If key6 Add if not key6:"+jedis.hincrBy("hash", "key6", 6));
System.out.println(" hash hash All key value pairs of are :"+jedis.hgetAll("hash"));
System.out.println(" take key6 The saved value plus an integer , If key6 Add if not key6:"+jedis.hincrBy("hash", "key6", 3));
System.out.println(" hash hash All key value pairs of are :"+jedis.hgetAll("hash"));
System.out.println(" Delete one or more key value pairs :"+jedis.hdel("hash", "key2"));
System.out.println(" hash hash All key value pairs of are :"+jedis.hgetAll("hash"));
System.out.println(" hash hash The number of key-value pairs :"+jedis.hlen("hash"));
System.out.println(" Judge hash Whether there is key2:"+jedis.hexists("hash","key2"));
System.out.println(" Judge hash Whether there is key3:"+jedis.hexists("hash","key3"));
System.out.println(" obtain hash The value in :"+jedis.hmget("hash","key3"));
System.out.println(" obtain hash The value in :"+jedis.hmget("hash","key3","key4"));
}
}
3. Business
public class TestMulti {
public static void main(String[] args) {
// Create client connection server ,redis The server needs to be turned on
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello", "world");
jsonObject.put("name", "java");
// Open transaction
Transaction multi = jedis.multi();
String result = jsonObject.toJSONString();
// Put an optimistic lock on transactions
jedis.watch(result);
try{
// towards redis Put in a piece of data
multi.set("json", result);
// Save another piece of data
multi.set("json2", result);
// This raises an exception , use 0 As divisor
int i = 100/0;
// If no exception is raised , Execute the command to enter the queue
multi.exec();
}catch(Exception e){
e.printStackTrace();
// If there is an anomaly , Roll back , Give up the business
multi.discard();
}finally{
System.out.println(jedis.get("json"));
System.out.println(jedis.get("json2"));
// Finally shut down the client
jedis.close();
}
}
}
边栏推荐
- 硅谷课堂笔记(中)
- [independent station operation] Shopify sellers: how to improve the store experience? Two moves are done!
- Summary of optimistic lock, pessimistic lock and distributed lock
- Redis缓存穿透和雪崩
- NFC介绍(2)
- 《PyTorch深度学习实践》-1-Overview
- [MySQL series] MySQL database foundation
- PMP备考指南之相关事项介绍
- Worthington肽合成应用丨胰凝乳蛋白酶方案
- Spark总结
猜你喜欢
2022-07-18 eliminate traversal processing of exists subquery
Worthington cholinesterase, butyryl related instructions
109. Ordered linked list transformation binary search tree ●●
Analysis sample of a video app
等保合规2022系列 | 一个中心+三重防护,助力企业等级保护建设更科学
How to make the signal of conductive slip ring better
Cellulase preparation scheme of Worthington plant protoplast
心肌黄酶丨Worthington克氏梭菌心肌黄酶的特性
Primary data structure - advantages and disadvantages of sequential list and linked list (leading bidirectional cyclic linked list) + knowledge of CPU cache.
2022.7.11-7.17 AI industry weekly (issue 106): just try your best
随机推荐
2022-07-15 mysql/stonedb子查询性能分析-FindOneInsidePack
[independent station operation] Shopify sellers: how to improve the store experience? Two moves are done!
10 baseline papers required for entry recommendation system
心肌黄酶丨Worthington克氏梭菌心肌黄酶的特性
Paper reading | point voxel CNN for efficient 3D deep learning
无动物型胶原酶丨Worthington的多种应用方案
Worthington cell separation optimization system (including cell separation guide)
2022-07-19 mysql/stonedb sub query hashjoin logic processing
Pytorch Deep Learning Practice - 1 - Overview
1. Openpyxl open workbook
2022-07-18 jenkins部署
2022-07-13 comparison of fast subquery and slow subquery execution of mysql/stonedb
如何围绕用户数字化运营?
Cell isolation study - Worthington Clostridium protease program
等保合规2022系列 | 今年,关于等保你该了解什么?
Time of regular release under test
Pdf to image and content reading
How to solve the "last mile of delivery" of community group purchase
ADS多频功放偏置网络设计
PMP Exam agile proportion soared, how to prepare for the exam?