Redis key Operation Command "learning notes"

Source: Internet
Author: User

Open service

$sudo./src/redis-server &

Key operation

* Define and assign a variable number of apples
$ redis-cli Set Dangcheng::apple::count 152
Ok

* Get the number of apples just now
$ redis-cli Get Dangcheng::apple::count
"152"

* When the number of apples increases by 1 incr keyword is actually shorthand for increment
$ redis-cli incr Dangcheng::apple::count
(integer) 153

* When Apple comes in a box, and this box of apples altogether 7 we need to add the specified number of keywords is Incrby
$ redis-cli Incrby Dangcheng::apple::count 7
(integer) 160

* If set a PHP array we can use PHP function serialize to serialize the object and write
$age =array ("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "43");
$age _str = Seriallze ($age);//writes this result to Redias
$age = Unseriallze ($age _str);//get we can use Unseriallze deserialization to convert it to a PHP object

EXISTS Key is present

Presence returns 1
$ REDIS-CLI exists Dangcheng::apple::count
(integer) 1
Does not exist return 0
$ REDIS-CLI exists Dangcheng::apple::color
(integer) 0

The dump serialization value does not contain any life cycle information

Does not exist return nil
$ REDIS-CLI Dump Dangcheng::apple::color
(nil)
Returning serialization results
$redis-CLI Dump Dangcheng::apple::count
"\x00\xc1\xa0\x00\x06\x00\xb0t6\xdat\x99#\xa2"

Expire and TTL setting lifetimes and viewing time remaining

The TTL command is the remaining of the query lifetime, and does not set the time to live return-1 represents a permanent
$ redis-cli TTL dangcheng::apple::count
(integer)-1
Use expire to set the lifetime remaining 366 seconds
$ redis-cli Expire Dangcheng::apple::count 366
(integer) 1
It takes 127 seconds to write this.
$ redis-cli TTL dangcheng::apple::count
(integer) 239
PS1: Also the Pexpire command is the same as the expire function, except that the setting is in milliseconds
*PS2: Also the Pttl command is the same as the TTL function, except that the display is in milliseconds

Expireat

This command differs from expire to set the remainder, but instead directly sets the current key when the parameter is the Linux timestamp representation
The Ps:pexpireat command is equivalent to the Expireat command, and the difference is the UNIX timestamp of the unit precision to the number of milliseconds

Keys to find all the keys that match the pattern of the given patterns

KEYS * matches all keys in the database.
KEYS H?llo matches Hello, Hallo and Hxllo.
KEYS Hllo matches Hllo and Heeeeello.
The KEYS H[ae]llo matches hello and Hallo, but does not match Hillo.
Special symbols separated by \
$ redis-cli keys Dangcheng::*
1) "Dangcheng::apple::color"
2) "Dangcheng::apple::count"

Migrate migrating an object in the current Redis instance to an instance of the corresponding port under another host "Data Migration"

Create another Redis service on the native 8888 port
$ redis-server–port 8888 &
[1] 13086
exists in the original instance
$ redis-cli
127.0.0.1:6379> keys Dangcheng::*
1) "Dangcheng::apple::color"
2) "Dangcheng::apple::count"

Data not found in the instance under Port 8888
$ redis-cli-p 8888
127.0.0.1:8888> Keys Dangcheng

(empty list or set)
The count of data in the source instance is passed to the instance of port 8888
127.0.0.1:6379> migrate 127.0.0.1 8888 dangcheng::apple::count 0-1
Ok
Count in the source instance is missing, proving that the source of the transfer is erased
127.0.0.1:6379> Keys Dangcheng
1) "Dangcheng::apple::color"
Found in the example under Port 8888.
127.0.0.1:8888> Get Dangcheng::apple::count
"25"

Move migrates data from one DB space to another DB space

Choosing a dbspace for index0
127.0.0.1:6379> Select 0
Ok
127.0.0.1:6379> keys *
1) "Dangcheng::apple::color"
2) "Test::first"
Moving to Index1 's dbspace
127.0.0.1:6379> Move Test::first 1
(integer) 1
127.0.0.1:6379> keys *
1) "Dangcheng::apple::color"
127.0.0.1:6379> Select 1
Ok
127.0.0.1:6379[1]> keys *
1) "Test::first"
We saw it, and we did find it in the Dbsopace1.
127.0.0.1:6379[1]> Get Test::first
"556779"
PS: It is important to note that if Target DB has the same key then the move will not succeed

Object Objects Manipulation

* Returns the number of times the stored value is referenced by the given key. This command is primarily used for debugging
127.0.0.1:6379[1]> Object RefCount Test::first
(integer) 1

  • Returns the internal representation (representation) used for the value stored by the given key lock.
    127.0.0.1:6379[1]> Object Encoding Test::first
    "Int"
    127.0.0.1:6379[1]> set str ' Hello redis! '
    Ok
    127.0.0.1:6379[1]> Object Encoding str
    "Raw"
    returns the idle time (idle, not read, or written) of the given key since it was stored, in seconds
    127.0.0.1:6379[1]> Object Idletime Str
    (integer) 236
    objects can be encoded in several ways:
    strings can be encoded as raw (generic string) or int (to save memory, Redis encodes a 64-bit signed integer represented by a string as an integer).
    The list can be encoded as ziplist or LinkedList. Ziplist is a special representation for saving smaller list spaces.
    The collection can be encoded as Intset or Hashtable. Intset are special representations of small sets that store only numbers.
    The hash table can be encoded as Zipmap or Hashtable. Zipmap is a special representation of a small hash table.
    * Ordered collections can be encoded as ziplist or skiplist formats. Ziplist is used to represent small, ordered sets, while Skiplist is used to represent an ordered set of any size.Persist the timeout period of a key into permanentto set the time to live for an object using expire
    127.0.0.1:6379[1]> expire Test::first 3600
    (integer) 1
    Query Time remaining
    127.0.0.1:6379[1]> TTL Test::first
    (integer) 3597
    127.0.0.1:6379[1]> persist Test::first
    (integer) 1
    * It does become a persistent key
    127.0.0.1:6379[1]> TTL Test::first
    (integer)-1

*randomkey randomly extracts a key
127.0.0.1:6379> Randomkey
"Dangcheng::apple::color2"
127.0.0.1:6379> Randomkey
"Dangcheng::apple::color"
127.0.0.1:6379> Randomkey
"Dangcheng::apple::color1"

Mset[multi Set] Batch setup

127.0.0.1:8888> mset a ' AAA ' B ' BBB ' C ' CCCC '
Ok
Query all keys
127.0.0.1:8888> keys *
1) "Dangcheng::apple::count"
2) "O"
3) "A"
4) "B"
5) "C"
Emptying the Dbspace
127.0.0.1:8888> Flushdb
Ok
* View all, obviously no more
127.0.0.1:8888> keys *
(empty list or set)

Rename Change key Name

127.0.0.1:6379> set a ' AAA '
Ok
127.0.0.1:6379> Rename a B
Ok
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379> Get B
"AAA"

Renamenx Target key Once existing will not be overwritten only the empty key can be the target

127.0.0.1:6379> Renamenx b C
(integer) 0
127.0.0.1:6379> get C
"66"
127.0.0.1:6379> Get B
"BB"
127.0.0.1:6379> Renamenx b D
(integer) 1
127.0.0.1:6379> Get D
"BB"
127.0.0.1:6379> GETB
(Error) ERR unknown command ' GETB '
127.0.0.1:6379> Get B
(nil)

Dump&restore Serialization and deserialization

127.0.0.1:6379> Dump C
"\x00\xc0b\x06\x00\xac\x15y\x1ao127.0.0.1:6379> get C
"66"
Serializing a value in C
127.0.0.1:6379> Dump C
"\x00\xc0b\x06\x00\xac\x15y\x1ao
creates a new key by deserializing a value NEWC
127.0.0.1:6379> restore NEWC 0 "\x00\xc0b\x06\x00\xac\x15y\x1aook
127.0.0.1:6379> Get NEWC
"66"

sort [Asc|desc] [alpha][limit] Order a list by default ASC, use Alpha to sort strings, and limit with limits

*lpush is the command used to create a list
127.0.0.1:6379> Lpush sort_list 15 2 45 20 16 3 7 9
(integer) 8
127.0.0.1:6379> Sort Sort_list
1) "2"
2) "3"
3) "7"
4) "9"
5) "15"
6) "16"
7) "20"
8) "45"
127.0.0.1:6379> Sort sort_list ASC
1) "2"
2) "3"
3) "7"
4) "9"
5) "15"
6) "16"
7) "20"
8) "45"
127.0.0.1:6379> Sort Sort_list desc
1) "45"
2) "20"
3) "16"
4) "15"
5) "9"
6) "7"
7) "3"
8) "2"

Type view Key's data type

* @return None (key does not exist) |string (string) |list (list) |set (collection) |zset (ordered set) |hash (hash table)
127.0.0.1:6379> type Sort_list
List
127.0.0.1:6379> Type C
String

Redis key Operation Command "learning notes"

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.