Java Operation Redis described in detail

  • 2020-11-18 06:14:22
  • OfStack

1. Introduction

Redis is an open source (BSD licensed), in-memory ES5en-ES6en storage system that can be used as a database, cache, and messaging middleware.

2. Operation of key

The first step is to establish the connection Jedis jedis = new Jedis("127.0.0.1", 6379), then you can operate on string, set, zset, hash.


// right key The test of 
public void keyTest() {
	System.out.println(jedis.flushDB());
	// Empty data 
	System.out.println(jedis.echo("hello"));
	// print hello
	System.out.println(jedis.exists("foo"));
	// judge key If there is a 
	jedis.set("key1", "values1");
	jedis.set("key2", "values2");
	System.out.println(jedis.exists("key1"));
	// judge key If there is a 
	String randomKey = jedis.randomKey();
	// choose 1 A random key
	System.out.println("randomKey For the : " + randomKey);
	jedis.expire("key1", 60);
	// Time to live 
	System.out.println(jedis.pttl("key1"));
	// The rest of your survival time 
	// remove key Expiration time 
	jedis.persist("key1");
	//  To obtain key The type of , "string", "list", "set" "none" none said key There is no 
	System.out.println("type The type of : " + jedis.type("key1"));
	//key The type of 
	//  export key The value of the 
	String value = jedis.get("key1");
	System.out.println(value);
	//  will key rename 
	jedis.renamenx("key1", "keytest");
	System.out.println("key1 If there is a : " + jedis.exists("key1"));
	//  To determine whether there is 
	System.out.println("keytest If there is a : " + jedis.exists("keytest"));
	//  To determine whether there is 
	//  Query-Matched key
	// KEYS *  Matches all in the database  key  . 
	// KEYS h?llo  matching  hello  .  hallo  and  hxllo  And so on. 
	// KEYS h*llo  matching  hllo  and  heeeeello  And so on. 
	// KEYS h[ae]llo  matching  hello  and  hallo  But it doesn't match  hillo  . 
	//  Special symbol  \  Separated. 
	Set<string> set = jedis.keys("k*");
	// Get all relevant key keys methods 
	System.out.println(set);
	jedis.del("key1");
	//  delete key del methods 
	System.out.println(jedis.exists("key1"));
}

3. String data type


set mystr "hello world!" // Set the string type 
get mystr // Read string type 
 
 Perform numeric manipulation on a string 
127.0.0.1:6379> set mynum "2"
OK
127.0.0.1:6379> get mynum
"2"
127.0.0.1:6379> incr mynum
(integer) 3
127.0.0.1:6379> get mynum
"3"

The operation code of Java is:


// right string Operational test 
public void stringTest() {
	jedis.set("hello", "hello");
	//set
	System.out.println(jedis.get("hello"));
	//get
	//  use append  Add to the string 
	jedis.append("hello", " world");
	// additional  append methods 
	System.out.println(jedis.get("hello"));
	// set Overwrite string 
	jedis.set("hello", "123");
	System.out.println(jedis.get("hello"));
	//  Set expiration time 
	jedis.setex("hello2", 2, "world2");
	System.out.println(jedis.get("hello2"));
	try {
		Thread.sleep(3000);
	}
	catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(jedis.get("hello2"));
	// 1 Add multiple times key-value right 
	jedis.mset("a", "1", "b", "2");
	//  To obtain a and b the value
	List<string> valus = jedis.mget("a", "b");
	System.out.println(valus);
	//  Batch delete 
	jedis.del("a", "b");
	System.out.println(jedis.exists("a"));
	System.out.println(jedis.exists("b"));
}

4. List data type

lists in redis is not an array on the underlying implementation, but a linked list.

Series 1 Operations: RPUSH, LPUSH, LLEN, LRANGE, LPOP and RPOP.

We can use LPUSH to insert a new element to the left of lists, RPUSH to insert a new element to the right of lists, and LRANGE to specify a range from lists to extract elements.


// new 1 a list called mylist , and inserts elements at the head of the list "1"
127.0.0.1:6379> lpush mylist "1"
// Returns the current mylist Number of elements in 
(integer) 1
// in mylist Insert element on the right "2"
127.0.0.1:6379> rpush mylist "2"
(integer) 2
// in mylist Insert element to the left "0"
127.0.0.1:6379> lpush mylist "0"
(integer) 3
// list mylist From the number in 0 The serial number 1 The elements of the 
127.0.0.1:6379> lrange mylist 0 1
1) "0"
2) "1"
// list mylist From the number in 0 To the bottom first 1 An element 
127.0.0.1:6379> lrange mylist 0 -1
1) "0"
2) "1"
3) "2"

The operation code of Java is:


public void listTest() {
	String key = "mylist";
	jedis.del(key);
	// Let me delete that 
	//  Queue add element 
	jedis.rpush(key, "aaaa");
	jedis.rpush(key, "aaaa");
	jedis.rpush(key, "bbbb");
	jedis.rpush(key, "cccc");
	jedis.rpush(key, "cccc");
	// The queue length 
	System.out.println("lenth: " + jedis.llen(key));
	//  Print queue from index 0 Here we go, to the bottom 1 Number of (all elements) 
	System.out.println("all elements: " + jedis.lrange(key, 0, -1));
	//  The index for 1 The elements of the 
	System.out.println("index of 1: " + jedis.lindex(key, 1));
	//  Set the queue 1 The value of the element, when index It returns when it goes out of range 1 a error . 
	jedis.lset(key, 1, "aa22");
	System.out.println("index of 1: " + jedis.lindex(key, 1));
	//  Join the queue from the right side of the queue 1 An element 
	jedis.rpush(key, "-2", "-1");
	//  First, -2 After, -1 In the queue 
	System.out.println("all elements: " + jedis.lrange(key, 0, -1));
	//  Join the queue from the left of the queue 1 Three or more elements 
	jedis.lpush(key, "second element", "first element");
	//  First, second
	// element After, first
	// elementF In the queue 
	System.out.println("all elements: " + jedis.lrange(key, 0, -1));
	//  Get out of the queue on the right 1 An element 
	System.out.println(jedis.rpop(key));
	//  Get out of the queue on the left 1 An element 
	System.out.println(jedis.lpop(key));
	System.out.println("all elements: " + jedis.lrange(key, 0, -1));
	// count > 0:  The value removed from beginning to end is  value  The elements, count Is the number of removals. 
	// count < 0:  Remove value from tail to head  value  The elements, count Is the number of removals. 
	// count = 0:  Remove all values of zero  value  The element. 
	jedis.lrem(key, 1, "cccc");
	System.out.println("all elements: " + jedis.lrange(key, 0, -1));
	//  The element on the far right will also be included.   if start than list When the index is large, it returns 1 An empty list. 
	//  if stop than list When the actual tail is larger, Redis Think of it as the last 1 The subscripts of the elements. 
	System.out.println(jedis.lrange(key, 0, 2));
	System.out.println("all elements: " + jedis.lrange(key, 0, -1));
	//  Deletes elements outside the interval 
	System.out.println(jedis.ltrim(key, 0, 2));
	System.out.println("all elements: " + jedis.lrange(key, 0, -1));
}

5. Set type

The set of redis is an unordered set with no sequence of elements.
Series 1 Operations: SADD, SREM, SISMEMBER, SMEMBERS and SUNION.
Collection-related operations are also abundant, such as adding new elements, deleting existing elements, taking intersection, union, difference set, and so on.


// To the collection myset add 1 A new element "one"
127.0.0.1:6379> sadd myset "one"
(integer) 1
127.0.0.1:6379> sadd myset "two"
(integer) 1
// List the collection myset All the elements in 
127.0.0.1:6379> smembers myset 
1) "one"
2) "two"
// Judge element 1 Whether or not in the set myset In the back 1 Said there is 
127.0.0.1:6379> sismember myset "one"
(integer) 1
// Judge element 3 Whether or not in the set myset In the back 0 Nonexistence 
127.0.0.1:6379> sismember myset "three"
(integer) 0
// new 1 A new set yourset
127.0.0.1:6379> sadd yourset "1"
(integer) 1
127.0.0.1:6379> sadd yourset "2"
(integer) 1
127.0.0.1:6379> smembers yourset
1) "1"
2) "2"
// You take the union of two sets 
127.0.0.1:6379> sunion myset yourset 
1) "1"
2) "one"
3) "2"
4) "two"

Java operation code:


public void setTest() {
	//  Empty data 
	System.out.println(jedis.flushDB());
	String key = "myset1";
	String key2 = "myset2";
	//  Collection add element 
	jedis.sadd(key, "aaa", "bbb", "ccc");
	jedis.sadd(key2, "bbb", "ccc", "ddd");
	//  Gets the number of elements in the collection 
	System.out.println(jedis.scard(key));
	//the number count of the set
	//  Gets the intersection of two collections and stores them in 1 A key result set 
	jedis.sinterstore("destination", key, key2);
	System.out.println(jedis.smembers("destination"));
	//  Gets the union of two sets and stores it in 1 A key result set 
	jedis.sunionstore("destination", key, key2);
	System.out.println(jedis.smembers("destination"));
	// key1 In the collection, key2 Collection does not have elements and is stored in 1 A key result set 
	jedis.sdiffstore("destination", key, key2);
	System.out.println(jedis.smembers("destination"));
	//  Determine if an element is 1 A member of a set 
	System.out.println(jedis.sismember(key, "aaa"));
	//  from key Random access from the set 1 An element 
	System.out.println(jedis.srandmember(key));
	// aaa from key Move to the key2 A collection of 
	jedis.smove(key, key2, "aaa");
	System.out.println(jedis.smembers(key));
	// Gets the elements in the collection 
	System.out.println(jedis.smembers(key2));
	//  Delete and get 1 The members of the set 
	System.out.println(jedis.spop(key));
	//  Delete from the collection 1 Three or more elements 
	jedis.srem(key2, "ccc", "ddd");
	System.out.println(jedis.smembers(key2));
}

6. Sorted Sets type

We all call the ordered set in redis zsets


// new 1 An ordered set myzset And join 1 An element baidu.com And the ordinal number given to it is 1
127.0.0.1:6379> zadd myzset 1 baidu.com 
(integer) 1
// to myzset In the new 1 An element 360.com And the ordinal number given to it is 3
127.0.0.1:6379> zadd myzset 3 360.com 
(integer) 1
// to myzset In the new 1 An element google.com And the ordinal number given to it is 2
127.0.0.1:6379> zadd myzset 2 google.com 
(integer) 1
// list myzset All of the elements, while listing its serial number, can be seen myzset It's already in order. 
127.0.0.1:6379> zrange myzset 0 -1 with scores 
1) "baidu.com"
2) "1"
3) "google.com"
4) "2"
5) "360.com"
6) "3"
// List only myzset The elements of the 
127.0.0.1:6379> zrange myzset 0 -1
1) "baidu.com"
2) "google.com"
3) "360.com"

The operation code of Java is:


public void zsetTest() {
	//  Empty data 
	System.out.println(jedis.flushDB());
	String key = "mysortset";
	Map<string, double=""> scoreMembers = new HashMap<>();
	scoreMembers.put("aaa", 1001.0);
	scoreMembers.put("bbb", 1002.0);
	scoreMembers.put("ccc", 1003.0);
	//  Add data 
	jedis.zadd(key, 1004.0, "ddd");
	jedis.zadd(key, scoreMembers);
	//  To obtain 1 The number of members in a sorted set 
	System.out.println(jedis.zcard(key));
	//  Returns an ordered collection of members within the specified range 0 Stands for ordered set number 1 Three members, to 1 Stands for ordered set number 2 Members, and so on. 
	//  Negative subscript, so -1 Said the last 1 A member, -2 Is the penultimate value 2 A member of 
	Set<string> coll = jedis.zrange(key, 0, -1);
	System.out.println(coll);
	//  The inverted collection of returned members in the specified range 
	coll = jedis.zrevrange(key, 0, -1);
	System.out.println(coll);
	//  Elements in the subscript 
	System.out.println(jedis.zscore(key, "bbb"));
	//  Remove elements 
	System.out.println(jedis.zrem(key, "aaa"));
	System.out.println(jedis.zrange(key, 0, -1));
	//  The number of members within a given range of values 
	System.out.println(jedis.zcount(key, 1002.0, 1003.0));
}

7. Hash type

hashes stores a mapping between strings and string values


// Set up a hash and assign a value 
127.0.0.1:6379> HMSET user:001 username antirez password P1pp0 age 34
OK
// List the contents of the hash 
127.0.0.1:6379> HGETALL user:001
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "34"
// Change something in the hash 1 A value 
127.0.0.1:6379> HSET user:001 password 12345
(integer) 0
// List the contents of the hash again 
127.0.0.1:6379> HGETALL user:001
1) "username"
2) "antirez"
3) "password"
4) "12345"
5) "age"
6) "34"

Java operation code:


set mystr "hello world!" // Set the string type 
get mystr // Read string type 
 
 Perform numeric manipulation on a string 
127.0.0.1:6379> set mynum "2"
OK
127.0.0.1:6379> get mynum
"2"
127.0.0.1:6379> incr mynum
(integer) 3
127.0.0.1:6379> get mynum
"3"
0

8. The transaction

A transaction is "a complete action, all or nothing".
Before talking about redis transaction, I would like to introduce four redis instructions, namely MULTI, EXEC, DISCARD and WATCH. These four instructions form the basis of redis's transaction processing.

1.MULTI is used to assemble 1 transaction;
2.EXEC is used to execute 1 transaction;
3.DISCARD is used to cancel 1 transaction;
4.WATCH is used to monitor 1 key, once the key has been changed before the transaction is executed, the transaction is cancelled.


set mystr "hello world!" // Set the string type 
get mystr // Read string type 
 
 Perform numeric manipulation on a string 
127.0.0.1:6379> set mynum "2"
OK
127.0.0.1:6379> get mynum
"2"
127.0.0.1:6379> incr mynum
(integer) 3
127.0.0.1:6379> get mynum
"3"
1

Java operation code is:


set mystr "hello world!" // Set the string type 
get mystr // Read string type 
 
 Perform numeric manipulation on a string 
127.0.0.1:6379> set mynum "2"
OK
127.0.0.1:6379> get mynum
"2"
127.0.0.1:6379> incr mynum
(integer) 3
127.0.0.1:6379> get mynum
"3"
2

Pipe operation Java code:


public void transactionPipelineTest() {
	Pipeline p = jedis.pipelined();
	// open 1 A pipe 
	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);
	System.out.println(pipeString);
	System.out.println(sose);
	p.sync();
	// submit 
	System.out.println("==========");
	System.out.println(p.get("fool"));
	System.out.println(p.zrange("foo", 0, -1));
	int soseSize = sose.get().size();
	Set<string> setBack = sose.get();
	System.out.println(soseSize);
	System.out.println(setBack);
	System.out.println(pipeString.get());
}

conclusion

That is all the details about Java operation Redis in this article. I hope it will be helpful to you. If there is any deficiency, please let me know. Thank you for your support to this site.


Related articles: