Redis Server Learning Notes (i)

Source: Internet
Author: User
Tags redis server

first, the installation of Redis :

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

Tar xzfredis-stable.tar.gz

CD redis-stable

Make

Make install

Second, a brief introduction of Redis:

1. Redis Executable Description:

Filename

Description

Redis-server

Redis Server

Redis-cli

Redis Command-line Client

Redis-benchmark

Redis Performance Testing Tools

Redis-check-aof

AOF File Repair Tool

Redis-check-dump

RDB File Check Tool

2. The Redis server starts in two ways:

I. Run the command line directly:

II. Use the Init script to start Redis.

Script Path : The Utils folder for the Redis source directory has an initialization script named Redis_init_script

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 "$" 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 initialization script.

CP/{redis Source path}/utils/redis_init_script/etc/init.d/redis_{Port number}

II. Edit the initialization script. Edit the 6th line of the script to modify the value of the Redisport variable so that it matches the port number in the previous example, for example: 7777

III. Create the necessary folders.

Directory Name

Description

/etc/redis

The configuration file that holds the Redis

/search/redis/Port number

Persistence files for Redis

Iv. Copy the Redis source code under the Redis.conf profile template to the/etc/redis directory, named after the port number. Example: 7777.conf.

V. Edit the. conf configuration file.

parameters

daemonize

yes

pidfile

/search/redis/redis _ Port number. PID

port

port number

Set the port number of the Redis listener

dir

/search/redi s/Port number

VI. Start Redis.

1. The Redis command line uses the command mode:

Way One:

Way two:

Iii. Summary of REDIS commands

Official website Command list:http://redis.io/commands (English)

1. Commands related to connection operation

· Quit: Close connection (connection)

· Auth: simple Password Authentication

2. Commands for value operations

· exists (key): Verify that a key exists

· del (key): Delete a key

Note:del the arguments of the command do not support wildcard characters, but can be combined with a Linux the pipeline and Xargs the command itself uses the delete all rules-compliant key.

For example, delete all the "User:" start with the key, you can execute

REDIS-CLI KEYS "user:*" | Xargs redis-cli DEL

Or

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

· Type (key): return value types

· keys (pattern): Returns all keys that satisfy the given pattern

Pattern wildcard character Type:

Symbol

Meaning

?

Match one character

*

Match any of the characters (including 0)

[ ]

Match any of the characters between the parentheses, you can use the "-" symbol to denote a range, such as a[b-d] that can match AB, AC, ad

\x

Matches the character X, which is used to escape symbols.

· Randomkey: Randomly returns a key in key space

· Rename (oldname,newname): Rename key from Oldname to newname and delete newname if newname exists

· Dbsize: Returns the number of keys in the current database

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

· TTL: Get the active time of a key

· Select (Index): Query by index

· Move (Key, Dbindex): Transfers the key from the current database to the database with the Dbindex index

· FLUSHDB: Delete all keys in the currently selected database

· Flushall: Delete all keys in all databases

3. Commands for string manipulation

· set (key, value): assigns a value to a string named key in the database

· get (Key): Returns the value of a string named key in the database

· Getset (key, value): Assigns the last value to a string named key

· Mget (Key1, Key2,..., key N): Returns multiple strings in the library (their names are key1,key2 ... ) of value

· SETNX (key, value): If a string with the name key does not exist, add a string to the library with the name key, value

· Setex (Key, Time,value): Adds a string to the library (named key, value =) at the same time, sets the expiration time

· Mset (Key1, Value1,key2, value2,... key N, Value N): assigns to multiple strings at the same time, string assigned value I, named key I

· Msetnx (key1,value1, Key2, value2,... key N, value N): If all strings with the name key I do not exist, a string is added to the library, the name key I is assigned value I

· incr (Key): string 1 operation named key

· Incrby (Key,integer): String with Name Key added integer

· DECR (Key): A string minus 1 operation with a name called key

· Decrby (Key,integer): string with a name called key reduces integer

· Append (key, value): String value with the name key appended value

· SUBSTR (Key, Start,end): Returns a substring of the value of string with the name key

4. Commands for list operations

· Rpush (key, value): Adds an element of value to the list at the end of the name key

· Lpush (key, value): Adds an element of value to the list header with the name key

· Llen (Key): Returns the length of the list named key

· Lrange (Key, Start,end): Returns the element between start and end in list named key (subscript starting from 0, same as below)

· LTrim (Key, Start,end): Intercepts the list named key, preserving the element between start and end

· Lindex (key, index): Returns the element of the index position in the list named key

· LSet (Key, Index,value): Assigns the element of the index position in the list named key to value

· Lrem (Key, Count,value): Removes the count of the elements in the list with the name key as value. Count is 0, removing all elements that have values of value, Count>0 removes the element with count values from start to end, Count<0 removes |count| values as value from the tail. Lpop (Key): Returns and removes the first element in the list named key Rpop (key): Returns and removes the tail element in list named key Blpop (Key1, Key2,... key N, timeout): The block version of the Lpop command. That is, when timeout is 0 o'clock, if a list with the name key I does not exist or the list is empty, the command ends. If timeout>0, if this is the case, wait for timeout seconds, and if the problem is not resolved, perform a pop operation on the list starting with keyi+1.

· Brpop (Key1, Key2,... key N, timeout): The block version of Rpop. Refer to the previous command.

· Rpoplpush (Srckey,dstkey): Returns and removes the tail element of the list named Srckey and adds the element to the head of the list named Dstkey

5. Commands for SET operations

· Sadd (Key, member): Adds an element to a set named Key member

· Srem (key,member): Removes the element in the set named Key member

· Spop (key): Randomly returns and deletes an element in a set with the name key

· Smove (Srckey,dstkey, member): Moves the member element from a collection named Srckey to a collection named Dstkey

· SCard (Key): Returns the cardinality of a set named key

· Sismember (key,member): Test whether the member is a set element with the name key

· Sinter (Key1,key2,... key N): Intersection

· Sinterstore (Dstkey,key1, Key2,... key N): intersection and save the intersection to a collection of Dstkey

· Sunion (Key1,key2,... key N): Seek and set

· Sunionstore (Dstkey,key1, Key2,... key N): Gather and save the set to the Dstkey collection

· Sdiff (Key1,key2,... key N): Differential set

· Sdiffstore (Dstkey,key1, Key2,... key N): Differential set and saving the difference set to the collection of Dstkey

· Smembers (Key): Returns all elements of a set with the name key

· Srandmember (key): Randomly returns an element of a set with the name key

6. Commands for Zset (sorted set) operation

· Zadd (Key, Score,member): Adds an element member,score for sorting to a zset named key. If the element already exists, the order of the element is updated according to score.

· Zrem (key,member): Removes the element in Zset with the name Key member

· Zincrby (Key,increment, member): If the element member already exists in Zset with the name key, the score of the element is incremented increment; otherwise, the element is added to the collection and its score value is increment

· Zrank (Key,member): Returns the rank (that is, index, starting from 0) of the member element in the Zset with the name key (the element has been sorted score from small to large), and returns "nil" if there is no member element

· Zrevrank (Key,member): Returns the rank (that is, index, starting from 0) of the member element in the Zset with the name key (the element has been sorted by score from large to small), and returns "nil" if there is no member element

· Zrange (Key, Start,end): Returns all elements of index from start to end in Zset with the name key (the element has been sorted by score from small to large)

· Zrevrange (Key,start, end): Returns all elements of index from start to end in Zset with the name key (the element has been sorted by score from large to small)

· Zrangebyscore (Key,min, max): Returns score >= min in Zset named key and all elements score <= Max Zcard (key): Returns the cardinality of Zset with the name Key Zscore (key , Element): Returns the Scorezremrangebyrank (Key, Min, max) of elements in Zset with the name key: delete Rank >= min and rank <= in Zset named key All elements of Max Zremrangebyscore (key, Min, max): Remove all elements of score >= min and score <= Max in Zset named key

· Zunionstore/zinterstore (Dstkeyn, Key1,..., KeyN, WEIGHTS W1,... WN, AGGREGATE sum| min| MAX): Sets and intersections of N Zset, and saves the last collection in Dstkeyn. For the score of each element in a collection, multiply the weight parameter for the aggregate operation before doing it. If weight is not provided, the default is 1. The default aggregate is sum, that is, the score of the elements in the result set is the value of the sum operation for all the corresponding elements of the collection, while Min and Max means that the score of the elements in the result set are the minimum and maximum values in the corresponding elements of all the collections.

7, the command of the hash operation

· Hset (Key, Field,value): Adds an element to the hash named key Field<->value

· Hget (Key, field): Returns the value of field corresponding to the hash named key

· Hmget (Key, field1,..., field N): Returns the value of field I corresponding to the hash named key

· Hmset (Key, field1,value1,..., field N, Value N): Adds an element to a hash named key Fieldi<->value I

· Hincrby (Key,field, Integer): Adds an integer to the value of field in the hash named key

· Hexists (Key,field): Whether a field with key field exists in a hash named key

· Hdel (Key, field): Delete The field with the key field in the hash named key

· Hlen (Key): Returns the number of elements in a hash with the name key

· Hkeys (Key): Returns all keys in a hash with the name key

· Hvals (Key): Returns the value corresponding to all keys in a hash with the name key

· Hgetall (Key): Returns all keys (field) and their corresponding value in a hash with the name key

8. Persistence

· Save: Synchronize data to disk

· Bgsave: Asynchronously saving data to disk

· Lastsave: Returns the UNIX timestamp when the data was last successfully saved to disk

· Shundown: Save data synchronously to disk, and then close the service

9. Remote Service Control

· Info: Provide information and statistics about the server

· Monitor: Live dump of received requests

· slaveof: Changing Replication policy settings

· Config: Configure the Redis server at run time

Redis Server Learning Notes (i)

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.