Redis series five: Redis Key Management and Redis database management

Source: Internet
Author: User
Tags redis cluster redis server

One, Redis Key Management 1 key Rename

Rename Oldkey newkey//format
Rename Oldkey NewKey//If Oldkey exists before it is overwritten
Set name James; set name1 Mike//Data initialization
Renamenx name name1//renaming fails only if NAME1 does not exist to rename

2 return random key

Randomkey//Return random key

3 keys Expire

Expire name:03 20//Key name:03 expires in 10 seconds
TTL name:03//view expires in seconds to timings when return-2 description deleted
Pttl name:03//view expires in milliseconds to meters
Set name:05 James//Initialize data
Pexpire name:05 20000//20000 MS (20S) expires
Expire name:06-2//direct expiration, same as Del


The set key expires at some point in time using a timestamp
Expireat name:04 1516971599//Set expires in 2018/01/27 20:59:59
Time-to-timestamp: URL http://tool.chinaz.com/Tools/unixtime.aspx

Hset user:01 name James//Initialize data
Expire USER:01 60//set to expire after 60S
TTL user:01//view Expiration Time remaining
Persist user:01//Remove Expiration
TTL user:1//Return-1 can be permanently queried without invalidating

Note: After the string reset value, expire is not valid,
Set name James
Expire name 50
TTL name
Set name James1//expire cancel at this time
TTL name//return-1, long-term valid

4. Migration of keys

Migrating part of the data to another Redis server

1, move key DB//reids has 16 libraries with a number of 0-15

Set name james1; Move name 5//migrate to 6th Library

Select 5;//Database switches to 6th library, get name can be taken to james1

This mode is not recommended for use in a production environment and can be played in the same reids

2, dump key;

Restore key TTL value//implements key migration for different Redis instances, ttl=0 represents no expiration time

Example: 192.168.1.111 on a server

Set name James;

Dump name; Get "\x00\x05james\b\x001\x82;f\" DHJ "

On the B server: 192.168.1.118

Restore name 0 "\x00\x05james\b\x001\x82;f\" DHJ "

Get name//return to James

3,migrate instruction migrates to other instance Redis, moves test to 118 on 1.111 server

Migrate

192.168.1.118

6379

Test

0

1000

Copy

Replace

Keys

Instructions

Destination IP to migrate

Port

Migrating Key values

Target Library

Timeout period

Do not delete the primary key after migration

The test key is migrated successfully regardless of the target library

Migrating multiple keys

5. Key traversal

Redis provides two commands to traverse all the keys

1, key full-scale traversal:

Mset Country China BJ name James//Set 3 string key-value pairs

Keys *//returns all keys, * matches any character more than one character

Keys *y//With the end key,

Keys N*e//begins with N and ends with E, returning the name

Keys N?me//? The question mark represents only one character to return name, global match

Keys n?m*//return name

keys [j,l]*//Return all keys starting with J l keys [j]ames full amount matching James

Considering that it is single-threaded, in the production environment is not recommended, if the key is more likely to block, if the key is small, you can

2, Progressive traversal

Mset a A b b c c D d e E F f g g H H i i j J K K L m m n n o o p p q q R s s t t u u v v w w x x y y z z//Initialize 26 characters Female key value pairs

String type:

Scan 0 Match N count 20//Match the key starting with N, fetch 20, start with scan 0 for the first time

The second time from the beginning of the cursor to fetch a key starting with n , equivalent to a page to fetch, when the last return 0 , the key is taken out

Note: Progressive traversal can effectively resolve blocking problems that the keys command can cause
In addition to the scan string: There are also the following
The SCAN command iterates over the database keys in the current database.
The Sscan command iterates over the elements in the collection key.
The HSCAN command is used to iterate over key-value pairs in hash keys.
The Zscan command iterates over elements in an ordered set, including element members and element scores.
Usage is the same as scan

Second, Redis database management

Select 0//A total of 16 libraries, 0--15, select Switch Database
Set name James
Select 1
Get name//isolated, not access, same as MySQL different library

The later versions of redis3.0 slowly weakened this feature, such as allowing only 0 databases in Redis cluster
Reason:
1,redis single-threaded, if using multiple libraries, these libraries use the same CPU, each other will have an impact
2, multi-database, debugging and operation of trouble, if there is a slow query, will affect the other library query speed
3, switch back and forth, easy to confuse

FLUSHDB: Empties only the current database's key-value pairs Dbsiz 0
Flushall: Clears the key-value pairs for all libraries (these two commands are used with CAUTION!!!!) )

Redis series five: Redis Key Management and Redis database management

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.