Redis Series-key Related main operation function __ function

Source: Internet
Author: User
Tags diff redis server

This blog mainly summarizes the common functions associated with key in Redis

1) Keys

Syntax: Keys pattern

Explanation: Finds all keys that match specified pattern patterns

[root@xsf001 ~]# redis-cli 
redis 127.0.0.1:6379> keys *  #所有key
 1) "Score"
 2) "Stu"
 3 "Score1" c5/>4) "Dest"
 5 "Lst.user"
 6) "Lst.tect"
redis 127.0.0.1:6379> keys Scor? 
1) "Score"
Redis 127.0.0.1:6379> keys scor[ee1]
1 "score"
2) Randomkey

Syntax: Randomkey

Explanation: Returns a random key

Redis 127.0.0.1:6379> randomkey
"score"
Redis 127.0.0.1:6379> "Randomkey"

3) exists

Syntax: Exists key

Explanation: Determine if a key exists

Redis 127.0.0.1:6379> exists score  #key存在 return 1
(integer) 1 redis 127.0.0.1:6379> exists SCOREFDA
# Key does not exist return 0
(integer) 0
4) Type

Syntax: Type key

Explanation: Returns the value type stored by key, return value: None "key does not exist", string,list, set, Zset and hash

Redis 127.0.0.1:6379> type score
zset
redis 127.0.0.1:6379> type Lst.user
list

5) Expire

Syntax: Expire key seconds

Explanation: Sets the lifetime of the key, in seconds, and is automatically deleted when key expires

Redis 127.0.0.1:6379> expire dest
(integer) 1 redis 127.0.0.1:6379> expire dest1
# key does not exist
(intege R) 0

6) TTL

Syntax: TTL key

Explanation: Gets the key to survive the time, if the key does not exist or does not set the lifetime time, returns-1

Redis 127.0.0.1:6379> expire diff
(integer) 1
redis 127.0.0.1:6379> ttl diff
(integer)
Redis 127.0.0.1:6379> ttl diff
(integer) 92
7) Persist

Syntax: Persist key

Explanation: Removes the lifetime of a given key

Redis 127.0.0.1:6379> ttl diff
(integer)
redis 127.0.0.1:6379> persist diff
(integer) 1
redis 127.0.0.1:6379> ttl diff
(integer)-1

8) Rename

Syntax: Rename key Newkey

Explanation: Rename key to Newkey

Redis 127.0.0.1:6379> smembers diff
1) "Zhangsan01"
redis 127.0.0.1:6379> rename diff diff01
ok< C13/>redis 127.0.0.1:6379> smembers diff
(empty list or set) Redis 127.0.0.1:6379> smembers diff01
1) "Zhangsan01"
redis 127.0.0.1:6379> rename diff diff01
(error) ERR no such key
Note: Returns an error when key and Newkey are the same or key does not exist, and rename overwrites the old value when Newkey already exists.

9) Renamenx
Syntax: Renamenx key Newkey

Explanation: When and only if Newkey does not exist, rename key

Redis 127.0.0.1:6379> Renamenx diff01 stu  #stu存在
(integer) 0
redis 127.0.0.1:6379> renamenx diff01 Diff #diff不存在
(integer) 1

) del

Syntax: Del key [Key ...]

Explanation: Delete one or more key

Redis 127.0.0.1:6379> del score1
(integer) 1
redis 127.0.0.1:6379> del Union diff AA  #key AA does not exist
(i Nteger) 2
One) move

Syntax: Move key db

Explanation: Moves the key to the specified db

Redis 127.0.0.1:6379> smembers stu #默认0
1) "Zhangsan01"
2) "Wangwu"
redis 127.0.0.1:6379> move Stu 1< c3/> #移动到 1
(integer) 1
redis 127.0.0.1:6379> smembers stu
(empty list or set)
Redis 127.0.0.1:6379 > select 1 #选择db 1
OK
redis 127.0.0.1:6379[1]> smembers stu
1) "Zhangsan01"
2) "Wangwu"
) sort

Syntax: sort key [by mode] [LIMIT offset count] [get mode [get pattern ...]] [asc| DESC] [ALPHA] [STORE destination]

Explanation: Returns or holds the sorted elements in a given list, set, ordered set key

Redis 127.0.0.1:6379> Sort Lst.tech limit 0 2 Alpha desc #按字符集排序
1) "TEC06"
2) "TEC05" Redis
127.0.0.1:6379 > Sort Lst.stud
1) "1"
2) "2"
3) "3"
redis 127.0.0.1:6379> sort lst.stud desc 1) "3" 2
) "2"
3) "1"
Redis 127.0.0.1:6379> keys stu.*
1) "Stu.name.2"
2) "Stu.name.3"
3) "STU.LEVEL.1"
4) "STU.LEVEL.2"
5) "STU.LEVEL.3"
6) "Stu.name.1"
redis 127.0.0.1:6379> sort Lst.stud by stu.level.* Desc get stu.level.* Get stu.name.*
1) "3"
2 "admin"
3 "2" 4 "
" Joe "
5)" 1 "6"
Jim "
Redis 127.0.0.1:6379> Sort Lst.stud by stu.level.* ASC get stu.name.*
1) "Jim"
2) "Joe"
3 "admin "

Sort is "awesome", but try not to let the Redis server sort large amounts of data. You can reduce the data you want to sort by setting thresholds, or move the sort operation forward, sort the Web server or each application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.