Redis Operations Basic Commands

Source: Internet
Author: User
Tags add time redis server

Redis- Remote Dictionary Server, which is an open source, written in ANSI C language, supports the network , can be based on memory and persistent log type, Key-value Database and provides multiple language APIs that we can use to build high-performance, extensible Web applications.

Has the following characteristics:

    • exceptionally fast: the Redis database is completely in memory , so processing is very fast and can perform approximately 110,000 episodes per second, about 81000 + records per second.
    • data persistence:Redis supports data persistence and can store in-memory data on disk for quick recovery in unexpected situations such as downtime.
    • supports rich data types: Redis has a rich set of data types compared to many other key-value pairs of storage databases.
    • Data consistency: all Redis operations are atomic, which ensures that redis servers accessed by two clients at the same time will get the updated values.
    • Multi-functional utility: Redis is a multi-utility tool that can be used in multiple uses such as cache, message, queue (Redis native support publish/subscribe), any transient data, applications such as Web application sessions, Web page hits count etc.

The first is the installation under Ubuntu :

Download:

[Email protected]:~# wget http://download.redis.io/releases/redis-3.0.0.tar.gz

Extract:

[Email protected]:~# tar zxvf redis-3.0.tar.gz

Compile and install:

[Email protected]:~ #cd redis-3.0.0 && make && make install

To view the installation version:

[Email protected]:~# src/redis-server-v

Basic components:

Components Use
Redis-server The startup program for the Redis server.
Redis-cli Redis command-Line Operations tool. Of course, you can also use Telnet to operate on its plain text protocol.
Redis-benchmark Redis Performance Testing tool to test the read and write performance of Redis in your system and in your configuration.
Redis-stat The Redis Status Detection Tool detects Redis current status parameters and latency status.

To start the Redis service:

[Email protected]:~# src/redis-server

To connect to Redis:

    [Email protected]:~# src/redis-cli    127.0.0.1:6379>

Basic parameter configuration:

Parameters function
Daemonize Whether to run the Redis service later in the daemon mode.
Port Redis service port, default 6379.
Timeout The request time-out period.
Requirepass Connect the database password.

redis.config The daemonize parameter defaults to no, in order for the Redis service to run in the background, we need to set the daemonize parameter to Yes.

Redis> SET Key value//overwrite the original value if the key value is the same, and clear the Save time of the original valueRedis> Setex Key seconds value//add time-to-live, in secondsRedis> GET Key//nil (not NULL) if key does not exist, error if value is non-string, because the get command can only get string type dataRedis> APPEND Key Appendvalue//Append to the existing string, if key does not exist, it is equivalent to set direct assignmentredis> MSET key value [Key1 value1 ...]//set multiple simultaneously, presence is overwrittenredis> MGET Key Key1 ...//get multiple key values at the same time, if a key does not exist, the current key returns nilFor example:127.0.0.1:6379>mget age Gender Sex1) "(2)" Ma N "3) (nil) Redis> SETRANGE Key Offset value//overwrites the current key with value, starting at offset offsets, and if offset is greater than the original value length, each gap in the middle is populated with a 0-byte "\x00"Redis> STRLEN Key//Returns the length of the string that is stored by the current key, when value is not a string, an error

Other directives:

instruction Use
Getset Sets the string value of the key and returns the old value.
GETRANGE Gets the substring of the string stored in a key.
Getbit Gets the bit on the specified offset for the string value stored by key.
Setbit Sets or clears a bit (bit) on the specified offset for the string value stored by key.
Setnx Set the value of key to value when and only if key does not exist.
Msetnx Set one or more key-value pairs at the same time, when and only if all of the given keys do not exist.
Psetex Similar to the Setex command, but it sets the lifetime of the key in milliseconds, rather than in seconds, as in the Setex command.
INCR Increase the numeric value stored in key by one.
Incrby Adds the value stored by key to the specified increment.
Incrbyfloat Adds the specified floating-point increment to the value stored in key.
Decr Subtract one of the numeric values stored in the key.
Decrby Adds the value stored by key to the specified increment.

Hset directive:

Use the hset command to set the value of the Domain field in the hash table key to value (that is, there are many key-value pairs stored by each key):

redis> hset key field value # set a new domain

Set multiple:

Hmset key field value [Field value ...]     If  It does not exist, an empty hash table is created and the Hmset operation is performed, and if the added domain already exists in the hash table, it will be overwritten.

Gets the specified field value under the specified key:

redis> hget key Field

Get multiple:

Redis> hmget key field1 field2 field3 ...             # The Order of return values is the same as the order of incoming parameters, and nil If field does not exist

Gets the field and value values for all field under the key (the return value format is field1 value1 and then Field2 value2 ... ), so the return value is twice times the length of the field:

Redis> Hgetall Key

Verify that the field under the given key is present:

redis> hexists key field    // presence return 1, no return 0

Returns all field under the current key:

Redis> Hkeys key       //  when key does not exist, returns an empty table

Returns the length of all field under the current key and:

Redis> Hlen key    // when key does not exist, returns 0

Delete field under a given key:

redis> hdel key field      // If it is a domain that does not exist, it will be ignored

Redis Operations Basic Commands

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.