Redis database (string and hash type)

Source: Internet
Author: User
Tags key string redis cluster

Redis database (string and hash type)

I. string type

String is the simplest type in Redis. A key corresponds to a value. redis string can contain any data object, such as JPG images or serialized objects.

The operations on the string type are as follows:

(1) set and get

Set is used to set a key-value pair. For example: set name ACdreamer, which means to set a key-value pair whose name is "ACdreamer". Instead, you need to obtain a key-value pair, use get name.

(2) setnx

Set the value corresponding to the key to a value of the string type. If the key already exists, 0 is returned. nx indicates not exist.

For example, set name ACdreamer, And then we use setnx name Jack. The name won't be overwritten here.

(3) setex

Set the value corresponding to the key to a string type value and specify the validity period of the key value. For example, if the validity period of a specified haircolor = red is 10 s, the set haircolor 10 red will expire after 10 s.

(4) setrange

Specifies the sub-string of the value of the specified key. For example, replace my QQ mailbox with a gmail mailbox.

Set mail ACdreamer@qq.com

Setrange mail 10 gmail

It indicates that the replacement starts from 10th subscripts, and the result after the replacement is ACdreamer @ gmailm. Obviously, this replacement is unreasonable.

(5) mset

If multiple key values are set at a time, if OK is returned successfully, all values are set. If 0 is returned, no value is set.

Mset name ACdreamer age 21 sex male

(6) msetnx

If multiple key values are set at a time, if OK is returned successfully, all values are set. If 0 is returned, no value is set, but no existing key exists. If one of the keys fails to be set, all keys fail to be set.

(7) getset

Set the new value of the key and return the old value of the key.

(8) getrange

Obtain the value of a key string, such as getrange name 0 5

(9) mget

Obtain the value of multiple keys at a time. If the value does not exist, the corresponding nil is returned. Example: mget name age sex

(10) incr

Add key values and return new values. Example: incr age

(11) incrby

Similar to incr, if a specified value is added, the key is set if the key does not exist and the original value is 0.

For example, incrby age 5

(12) decr and decrby

Similar to incr and incrby, this operation reduces the specified key.

(13) append

Append value to the specified string to return the length of the new string. For example, append name @ gmail.com

(14) strlen

The length of the value of the specified key.

Ii. hash type

The hash type of redis is a string type field and value ing table. It adds and deletes all O (1) operations, and hash is particularly suitable for storing objects, each field of an object is stored as a single string type. Storing an object as a hash type will consume less memory and facilitate access to the entire object. Hash is equivalent to a table in MySQL.

Hash Table operations:

(1) hset

Set the field value In the table, for example, hset table name ACdreamer

(2) hsetnx

If the value of the same field in the same hash table has been set, 0 is returned, indicating that the setting is not successful, which is similar to setnx in the string type.

(3) hmset

Batch setting: similar to mset in string, multiple fields can be set at the same time. For example

Hmset table name ACdreamer age 21 sex male

(4) hget and hmet

Hget returns the value of a single field in the hash table, for example, hget table name.

The value of multiple fields in the hash table is returned by the hmet table name age sex.

(5) hincrby

Add a certain value to the value of a specified field in the hash table, for example, hincrby table age 4.

(6) hexists

Checks whether a field in a hash table exists. If yes, 1 is returned. Otherwise, 0 is returned.

Example: hexists table name

(7) hlen

Returns the number of all existing fields in the specified hash table.

(8) hdel

Delete the specified field in the hash table.

(9) hkeys

Returns all fields in the hash table. Example: hkeys table

(10) hvals

Returns the values of all fields in the hash table. Example: hvals table

(11) hgetall

Obtains all field and value values in a hash table.

Install and test Redis in Ubuntu 14.04

Redis cluster details

Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis

Redis series-installation, deployment, and maintenance

Install Redis in CentOS 6.3

Learning notes on Redis installation and deployment

Redis. conf

Redis details: click here
Redis: click here

This article permanently updates the link address:

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.