Redis server Study Notes (1)

Source: Internet
Author: User

Redis server Study Notes (1)

I. Redis Installation:

Wget http://download.redis.io/redis-stable.tar.gz

Tar xzfredis-stable.tar.gz

Cd redis-stable

Make

Make install

Ii. Redis introduction:

1. Description of the Redis executable file:

File Name

Description

Redis-server

Redis Server

Redis-cli

Redis command line Client

Redis-benchmark

Redis Performance Testing Tool

Redis-check-aof

AOF file Repair Tool

Redis-check-dump

RDB File Check Tool

2. Two Methods for starting the Redis Server:

I. directly run the command line:

Ii. Use the initialization script to start Redis.

Script path: There is an initialization script named redis_init_script In the utils folder of the redis source code directory.

Script content:

#! /Bin/sh

#

# Simple Redis init. d script conceived towork on Linux systems

# As it does use of the/proc filesystem.

REDISPORT = 6379

EXEC =/usr/local/bin/redis-server

CLIEXEC =/usr/local/bin/redis-cli

PIDFILE =/var/run/redis _ $ {REDISPORT}. pid

CONF = "/etc/redis/$ {REDISPORT}. conf"

Case "$1" in

Start)

If [-f $ PIDFILE]

Then

Echo "$ PIDFILE exists, process isalready running or crashed"

Else

Echo "Starting Redis server ..."

$ EXEC $ CONF

Fi

;;

Stop)

If [! -F $ PIDFILE]

Then

Echo "$ PIDFILE does not exist, processis not running"

Else

PID = $ (cat $ PIDFILE)

Echo "Stopping ..."

$ CLIEXEC-p $ REDISPORT shutdown

While [-x/proc/$ {PID}]

Do

Echo "Waiting for Redis to shutdown ..."

Sleep 1

Done

Echo "Redis stopped"

Fi

;;

*)

Echo "Please use start or stop asfirst argument"

;;

Esac

Configuration method:

I. Copy the initialization script.

Cp/{redis source code path}/utils/redis_init_script/etc/init. d/redis _ {port number}

Ii. Edit the initialization script. Edit row 6th of the script and modify the REDISPORT variable value to make it consistent with the port number in the preceding example. For example: 7777

Iii. create necessary folders.

Directory Name

Description

/Etc/redis

Stores Redis configuration files

/Search/redis/port number

Store Redis persistent files

Iv. Copy the redis. conf configuration file template under the redis source code to the/etc/redis directory and name it with the port number. For example, 7777. conf.

V. Edit the. conf configuration file.

Parameters

Value

Description

Daemonize

Yes

Enable Redis to run in daemon mode

Pidfile

/Search/redis _ port number. pid

Set pid File Location of PID

Port

Port Number

Set the port number of the redis listener

Dir

/Search/redis/port number

Set the storage location of persistent files

Vi. Start Redis.

1. Redis command line command:

Method 1:

Method 2:

Iii. Redis command Summary

Official site command list: http://redis.io/commands (English)

1. Connection operation-related commands

· Quit: disconnect)

· Auth: Simple Password Authentication

2. commands for value operations

·Exists (key): Check whether a key exists.

·Del (key ):Delete a key

Note: the parameters of the DEL command do not support wildcards, but you can use the Linux pipeline and xargs command to delete all the keys that comply with the rules.

For example, if you delete all keys starting with "user:", you can execute

Redis-cli KEYS "user: *" | xargs redis-cli DEL

Or

Redis-cli DEL 'redis-cli KEYS "user :*"'

·Type (key): Type of the returned value

·Keys (pattern): Returns all keys that meet the specified pattern.

Pattern wildcard type:

Symbol

Description

?

Match one character

*

Match any (including 0) characters

[]

To match any character in the brackets, you can use the "-" symbol to represent a range. For example, a [B-d] can match AB, ac, and ad.

\ X

Match character x for escape characters.

· Randomkey: Random return of a key in the key space

· Rename (oldname, newname): rename the key from oldname to newname. If newname exists, delete the key represented by newname.

· Dbsize: returns the number of keys in the current database.

· Expire: Set the activity time of a key (s)

· Ttl: obtains the activity time of a key.

· Select (index): query by index

· Move (key, dbindex): transfers the key in the current database to a database with a dbindex.

· Flushdb: delete all keys in the selected Database

· Flushall: delete all keys in all databases

3. String operation commands

·Set (key, value ):Assign value to the string named key in the database

·Get (key ):Returns the value of a string named key in the database.

· Getset (key, value): Assign the last value to the string named key.

· Mget (key1, key2 ,..., Key N): returns multiple strings in the database (their names are key1, key2 ...) Value

· Setnx (key, value): If there is no string named key, add a string to the database, with the name key and value

· Setex (key, time, value): to add a string (Name: key, value: value) to the database, and set the expiration time

· Mset (key1, value1, key2, value2 ,... Key N, value N): assign values to multiple strings at the same time.

· Msetnx (key1, value1, key2, value2 ,... Key N, value N): If none of the strings whose names are key I exist, add the string to the database and assign the value of key I to value I.

·Incr (key ):Add 1 to a string named key

·Incrby (key, integer): Add integer to the string with the key name

·Decr (key ):The string minus 1 operation whose name is key

·Decrby (key, integer ):Reduce integer for a string named key

· Append (key, value): append value to the string value of the key

· Substr (key, start, end): return the substring of the value of string with the name of key.

4. List Operation commands

· Rpush (key, value): adds an element with the value at the end of the list named key.

· Lpush (key, value): adds an element with value to the list header named key.

· Llen (key): returns the length of the list named key.

· Lrange (key, start, end): returns the element between start and end in the list named key (subscript starts from 0, the same below)

· Ltrim (key, start, end): truncates the list named key and retains the elements between start and end.

· Lindex (key, index): returns the index position element in the list named key.

· Lset (key, index, value): assigns a value to the index position element in the list named key.

· Lrem (key, count, value): deletes the count elements with the value in the list named key. If the value of count is 0, all elements with the value are deleted. If the value of count is greater than 0, all elements with the value are deleted from the beginning to the end, count <0 from the end to the header | count | elements with a value. Lpop (key): return and delete the first element rpop (key) in the list named key: return and delete the ending element blpop (key1, key2 ,... Key N, timeout): The block version of The lpop command. That is, when the timeout value is 0, the command ends if the list named key I does not exist or the list is empty. If the timeout value is greater than 0, wait for timeout seconds in the case of the preceding situation. If the problem persists, perform the pop operation on the list starting with keyi + 1.

· Brpop (key1, key2 ,... Key N, timeout): rpop block version. Refer to the previous command.

· Rpoplpush (srckey, dstkey): returns and deletes the end element of the list named srckey, and adds the element to the header of the list named dstkey.

5. Set operation commands

· Sadd (key, member): add the element member to the set named key.

· Srem (key, member): deletes the element member from the set named key.

· Spop (key): Randomly returns and deletes an element in a set named key.

· Smove (srckey, dstkey, member): Move the member element from the set named srckey to the set named dstkey.

· Scard (key): returns the base number of a set named key.

· Sismember (key, member): Tests whether member is an element of a set named key.

· Sinter (key1, key2 ,... Key N): calculates the intersection.

· Sinterstore (dstkey, key1, key2 ,... Key N): calculates and saves the intersection to the dstkey set.

· Sunion (key1, key2 ,... Key N): returns the union.

· Sunionstore (dstkey, key1, key2 ,... Key N): calculates the Union set and saves the Union set to the dstkey set.

· Sdiff (key1, key2 ,... Key N): returns the difference set.

· Sdiffstore (dstkey, key1, key2 ,... Key N): calculates the difference set and saves the difference set to the dstkey set.

· Smembers (key): returns all elements of a set named key.

· Srandmember (key): Random return of an element of the set named key

6. commands for zset (sorted set) Operations

· Zadd (key, score, member): add the element member to the zset named key. score is used for sorting. If the element already exists, the sequence of the element is updated based on the score.

· Zrem (key, member): deletes the element member in the zset named key.

· Zincrby (key, increment, member): If the element member already exists in the zset named key, the score of this element is added with increment; otherwise, this element is added to the set, the score value is increment.

· Zrank (key, member): return the rank (index, starting from 0) of the member element in the zset (the element has been sorted by score in ascending order) with the key name ), if no member element exists, "nil" is returned"

· Zrevrank (key, member): return the rank (index, starting from 0) of the member element in the zset (the elements are sorted by score in ascending order) with the key name ), if no member element exists, "nil" is returned"

· Zrange (key, start, end): returns all the elements of index from start to end in the zset with the name of key (the elements have been sorted by score in ascending order ).

· Zrevrange (key, start, end): returns all the elements of index from start to end in the zset named key (the elements have been sorted by score in ascending order ).

· Zrangebyscore (key, min, max): returns all zcard (key) elements of the zset with the name of key, namely, score> = min and score <= max ): returns the base zscore (key, element) of the zset whose name is key: returns the scorezremrangebyrank (key, min, max) of the element in the zset whose name is key ): delete all zremrangebyscore (key, min, max) elements in the zset with rank> = min and rank <= max ): delete all the elements with score> = min and score <= max in the zset named key.

· Zunionstore/zinterstore (dstkeyN, key1 ,..., KeyN, WEIGHTS w1 ,... WN, aggregate sum | MIN | MAX): returns the Union and intersection of N zsets, and saves the last set in dstkeyN. For the score of each element in the Set, the score must be multiplied by the WEIGHT parameter before the AGGREGATE operation. If WEIGHT is not provided, the default value is 1. The default AGGREGATE value is SUM, that is, the score of the element in the result set is the SUM calculation value for all the corresponding elements in the Set, while MIN and MAX refer, the score of an element in the result set is the minimum and maximum values of all elements in the set.

7. commands for Hash operations

· Hset (key, field, value): Add an element field to the hash named key <-> value

· Hget (key, field): return the value corresponding to the field in the hash with the key name.

· Hmet (key, field1 ,..., Field N): return the value corresponding to field I in the hash with the key name.

· Hmset (key, field1, value1 ,..., Field N, value N): add the fieldi element to the hash named key <-> value I

· Hincrby (key, field, integer): adds an integer to the field value in the hash named key.

· Hexists (key, field): indicates whether the hash with the key as the field exists.

· Hdel (key, field): deletes the field in the hash key named key.

· Hlen (key): returns the number of elements in the hash key.

· Hkeys (key): returns all keys in the hash key

· Hvals (key): return the value corresponding to all keys in the hash with the key name.

· Hgetall (key): returns all the keys (fields) and their corresponding values in the hash key.

8. Persistence

· Save: synchronize and save data to the disk.

· Bgsave: asynchronously saves data to the disk.

· Lastsave: returns the Unix timestamp from which data is successfully saved to the disk.

· Shundown: synchronize and save the data to the disk, and then close the service.

9. Remote Service Control

· Info: provides server information and statistics.

· Monitor: Real-time dump of received requests

· Slaveof: changes the replication policy settings

· Config: configure the Redis server at runtime

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.