RedisAPI learning diary

Posted by jalperin on Thu, 17 Feb 2022 23:08:24 +0100

1, General characteristics of redis

  • The subscript of redis supports both positive and negative numbers+ 1 represents the first element- 1 indicates the last to last element. So you can make good use of this feature to traverse. For example, list, lrange list 1 -1 can traverse the list.
  • Redis's scope is a closed interval. Unlike java's, the scope of redis's scope is a left-hand package rather than a right-hand package.

2, KEY

redis has five data structures: String, List, set, Sorted Set and hash.
But why is there an API for six types of data structures in the API Manual: Key, String, List, Hash, Set and sorted Set?

1. Simple API similar to java

The reason why redis has a key API is that the underlying layer of redis should be Map < string, Object >, and then the key API is used to operate the Map, for example:

map.remove("keyName")  == del key "keyName"
map.get("key").getClass() == type "keyName"
map.forEach(item -> sysout(item.getKey)) == keys *
map.keyset().contails("keyName") == exists "keyName"

2. java like composite API

In addition to simple APIs similar to java, there are also composite APIs similar to multiple java statements, such as

keys "list*" == {
	List<> result = new ArrayList();
`	for(String key : map.keySet()){
		if(key Regular satisfaction "list*"){
			result.add(key)'
		}
		return result;
	}
}

rename key newkey == {
	if(!map.keySet().contains("key")){
		return "ERR no such key"
	}
	map.put("newKey", map.get("key"))
	return "OK"
}

renamenx key newkey == {
	if(!map.keySet().contains("key")){
		return "ERR no such key"
	}
	if(map.keySet().contains("newKey")){
		return;
	}
	map.put("newKey", map.get("key"))
	return "OK"
}

3. Life cycle related

However, the API of redis's key is similar to the Java API, but also has some new features.
For example, there is the concept of "expiration time"
ttl,pttl: time to live
expire,pexpire
expireat,pexpireat
persist

other

//TODO

In addition, redis has 16 libraries, so these key s can also be moved to other libraries
There are also some compound operations,

sort

Delete the list, except pop the item s one by one, and redis will not save the empty list and then delete the list; You can also delete the list with del key [listKey]

3, String

redis's string API refers to the API of string type for the object corresponding to the key!

Add, delete, modify and check

Add: set,setnx,setex,psetex
	Batch increase: mset,msetnx
 Delete: use key of API,del key "keyName"
Change: setRange,append,
Check: get,getRange,strlen
	Batch query: mget
 Compound operation: getset
 Increment and decrement of numeric string: decr,decrBy,incr,incrBy,incrByFloat
 Binary operation:
	Modify a bit: setBit
	Query a person: getBit
	Bitwise logical and or not: bittop
	Number of digits of query 1: bitCount

It is worth noting that
1.incr and incrBy support negative numbers, so incrBy val -2 is equivalent to decr val 2, which explains why the API does not have decrByFloat, because incrByFloat can also achieve the same function
2. For the set operation, if a numeric value (integer or floating-point type) is passed in, it will be automatically converted to a numeric string;
3. For get operation, if the type corresponding to key is not String, an error will be reported; However, if mget is used, if the part corresponds to String and the part does not correspond to String, the successful result will be returned, and the failed result will be null

4, Hash

The string API of redis refers to the API of hash type for the object corresponding to the key!

Add, delete, modify and check

Add: hset,hsetnx,hmset
 Delete: use key of API,del key "keyName",hdel
 Check: hget,hmget,hgetAll,hkeys,hvals,hlen,hexists
 Change:
Increment and decrement of numeric and numeric string: hincrby,hincrbyFloat

5, List

The string API of redis refers to the API of list type for the object corresponding to the key! It is worth noting that redis's list is a two-way linked list, so it can be used as either a stack or a linked list; Redis's list also has a feature that allows blocking values. If there is no value in the list, it can be blocked until there is a value.

Add, delete, modify and check

Add element: lpush,lpushx,rpush,rpushx,linsert
 Take the element: lpop,rpop,blpop,brpop
 Delete element: with key of API,del key "keyName",lrem,ltrim
 Check: llen,lindex
 Change: lset
 Compound operation: rpoplpush,brpoplpush

Lpush means that when the key to be operated does not exist, a list of keys will be created first, and then inserted; If the key already exists but its type is not list, an error will be reported; Lpushx operates only if and only if the key exists and is of type list. To put it simply, lpush has an error checking mechanism, and lpushx is sure to operate successfully, but the insertion may fail.

If blpop receives multiple key s, it will see which list has a value from left to right, and which has a value will be returned immediately. For example, blpop list1 list2 list3, where list1 has no value, list2 has value2, and list3 has value3, blpop list1 list2 list3 0 will return list2 value2. As for why we should return list2 value2 instead of directly returning value2, because blpop can receive multiple lists. If only value is returned, we don't know which list this value pops up from.

Rpop lpush can make the list of redis as a message queue and ensure security. And you can use rpop lpush to traverse the list instead of lrange to get all the item s of the list at once.

6, set

Add, delete, modify and check

Add element: sadd
 Take the element: smembers
 Delete element: srem,spop
 Other operations: sismember,smove(similar list of rpoplpush),sinter(Take intersection) sinterstore,sdiff(Take difference set) sdiffstore

The difference between spop, srem and srandmember: spop can only receive one key, and a random item in the key pops up (deleted); srem accepts one key and multiple items. It can pop up (delete) multiple items at one time, and returns the number of pop-up (delete) items; Srandmember can specify the genus, which is equivalent to executing spop multiple times

Why set does not provide an API for union sets: Union sets can be implemented through smembers+sadd. Smembers gets the elements of all sets, creates a new set, and sadds the set.

7, sorted set

The sorted set of redis is realized through the score of item. By default, it is sorted from small to large according to the score.

Add element: zadd
 Take the element:
Delete element: zrem,zremRangeByRank,zremRangeByScore
 Query operation: zcard(amount to set.size()),zcount(according to item of score Make statistics size,amount to zcard+Range filtering) zscore(query item Corresponding score) zrange,zrevrange(Query assignment score In scope item),zrangeByscore,zrangeByScore(zrange Pagination version of) zrank(Get ranking)
Other operations: zincrby,zinterStore(Take intersection) zunionstore(Union set)

Topics: Redis