Brothers Learn python---redis basics

Source: Internet
Author: User
Tags set set strong password

Redis Data Types
Redis supports five types of data:    string (string)    list (list)    set (set)    Zset (ordered set)    hash (hash) 
String (String)
The string is the most basic type of redis, and you can understand it as a type that is identical to memcached, a key that corresponds to a value. The string type is binary safe. This means that a Redis string can contain any data. For example, JPG images or serialized objects. The string type is the most basic data type of Redis, and a key can store up to 512MB.
Example
Redis 127.0.0.1:6379> Set name "Runoob" Okredis 127.0.0.1:6379> GET name "Runoob" in the above example we used Redis's SET and get commands. The key is name and the corresponding value is Runoob. Note: A key can store up to 512MB. --------------------------------------------SET command: Set a key and value, the key exists only overwrite, return OK > Set key value for example: >set name Zhangsan   ? Get command: Gets the value of a key, return value > Get keyFor example: >get name? SETNX command: Sets a nonexistent key and value (prevents overwriting), > setnx key value    Returns 0 if the key already exists indicates a failure?   SETEX command: Sets a key and value for a specified validity period (in seconds)   > Setex key [Effective time] value   For example: >setex color red   No write valid time means permanent, etc. Price at set?    SETRANGE command: Replace substring (replacement length is determined by substring length)   > SetRange key position substring   > setrange name 4 aa Replace the 4th position of the name key corresponding value?   mset command: Bulk set key and value, successful return OK   > Mset key 1 value 1 key 2 value 2 Key 3 value 3 ...?   MSETNX command: Bulk set non-existent keys and values, successful return OK   > msetnx key 1 value 1 key 2 value 2 Key 3 value 3 ...?   Getset Command: Get the original value and set the new value   > Getset key new value?   GetRange command: Gets the value of the specified range   >getrange key 0 4    //Gets the value at the specified 0 to 4 position?   mget command: Bulk get value   >mget key 1 key 2 Key 3 ...?   INCR Command: Specify the value of the key to do the Gaga operation, return the result after adding.   >       For example: >incr kid   Incrby command: Set a key plus specified value   > Incrby key m  //where m can be a positive Integer or negative integer?   DECR Command: Specifies the value of the key to do the subtraction operation, returning the result after the subtraction.   > DECR key       Example: >DECR kid   Decrby command: Set a key minus the specified value   > Decrby key m  //where M Can be a positive integer or a negative integer?   APPEND command: Append value to the string of the specified key, returning the length of the new string value   >appEnd key Append string?   strlen length   >strlen key name  //returns the corresponding value.     del command: Delete the specified key, return 1   > del key if successful

list (lists)
The Redis list is a simple list of strings, sorted by insertion order. You can add an element to the head of the list (to the left) or to the tail (to the right).
Example
Redis 127.0.0.1:6379> lpush runoob redis (integer) 1redis 127.0.0.1:6379> lpush runoob mongodb (integer) 2redis 127.0 .0.1:6379> lpush runoob rabitmq (integer) 3redis 127.0.0.1:6379> lrange runoob 0 101) "RABITMQ" 2) "MongoDB" 3) "Redis"   The Redis 127.0.0.1:6379> list can store up to 232-1 elements (4294967295, each of which can store more than 4 billion).?-------------------------------------------- The list can be used as a "stack" or as a "queue". Action: >lpush list1 "World"//pressing a string in List1 head >lpush list1 "Hello"//pressing a string in the List1 header >lrange list1 0-1//Get in List1 Content0: The beginning-1 means the end. >rpush list2 "World"//pressing a string in the List2 tail >rpush list2 "Hello"//pressing a string in List2 tail >lrange l Ist2 0-1//Get List2 in content 0: Represents the beginning-1 means the end.? >linsert List2 Before Hello there add a string before or after the key corresponds to a specific position in the list? >lset Lis T2 1 "Four" modifies the value at the specified index position? >lrem list2 2 "Hello"//delete the first two hello values >lrem list2-2 "Hello"//delete after two hello values >lrem list2 0 "Hello"//delete all Hello values? >ltrim MYLIST8 1 3 //Delete the value outside this range? >lpop List2//remove element from List2 's head and return delete element >rpop List2//delete element from Tail of list2 and return delete element &G T;rpoplpush list1 list2 //Remove an element from the tail of the List1 to the List2 head. and return? >lindex List2 1//Returns the element at index position in List2 >llen List2//Returns the length of List2   
set (unordered collection)
Redis's set is an unordered collection of type string. The collection is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1). The Sadd command adds a string element to the set set of key corresponding to successfully returning 1 if the element has returned in the collection 0,key the corresponding set does not have a return error. Sadd Key Member
instance
Redis 127.0.0.1:6379> sadd runoob redis (integer) 1redis 127.0.0.1:6379> sadd runoob mongodb (integer) 1redis 127.0.0 .1:6379> sadd runoob rabitmq (integer) 1redis 127.0.0.1:6379> sadd runoob rabitmq (integer) 0redis 127.0.0.1:6379 > Smembers runoob?1) "RABITMQ" 2) "MongoDB" 3) "Redis" Note: The above example is RABITMQ added two times, but the second inserted element is ignored based on the uniqueness of the elements within the collection.  The maximum number of members in the collection is 232-1 (4294967295, each of which can store 40多亿个 members).?------------------------------->sadd MySet "Hello"/Add an element to MySet Successfully returned 1, failed (repeat) returned 0? >smembers MySet//Gets all the elements in the MySet (the result is unordered)? >srem MySet "One"//deleted from MySet a successful return 1, failed (not present) returned 0? >spop MySet//random return and delete an element in MySet >srandmember myset//Random get an element in MySet, but not delete it? > Smove myset1 myset2 zhangsan: Move myset1 in Zhangsan > Myset2 scard return myset1 > Myset1 sismember myset: Sentenced Is the broken Zhang San in the MySet? >sdiff Myset1 Myset2//Returns the difference set of two sets with Myset1 as the standard, getting the non-existent in Myset2. >sdiffstore dstset myset1 Myset2 ...//Returns the difference set of all collections and saves them in Dstset? >sinter myset1 Myset2 Myset3 ...//returns the intersection of N collections >sinterstore Dstset mySet1 Myset2 ...//returns the intersection of N collections and stores them in Dstset? > Sunion myset1 Myset2 ...//returns the set of all collections > Sunionstore dstset myset1 myset2//Returns the set of all collections and stores them in Dstset
Zset (sorted set: Ordered set)
Example
Redis 127.0.0.1:6379> zadd runoob 0 redis (integer) 1redis 127.0.0.1:6379> zadd runoob 0 mongodb (integer) 1redis 127. 0.0.1:6379> zadd runoob 0 rabitmq (integer) 1redis 127.0.0.1:6379> zadd runoob 0 rabitmq (integer) 0redis 127.0.0.1:63 79> zrangebyscore Runoob 0 1000?1) "Redis" 2) "MongoDB" 3) "RABITMQ"?----------------------------------------? > Zadd zset 1 One add one to Zset, sort 1 sort > Zrem zset one: delete one in Zset? > Zincrby zset 2 One: If one exists, the order is incremented by 2, and if one does not exist, then 2? > Zrank Zset One: Return one ranking in Zset (from small to Large) > Zrevrank zset One: Return one in Zset ranking (from largest to smallest sort)? > Zrange zset 0-1 withscores: Sort According to score (sort by score from small to large) > Zrevrange zset 0-1 withscores: Sort According to score (sort by score from large to small)? > Zrangebyscore zset 2 3 withscores: Returns the element in the collection score at a given interval (contains 2 and 5) > Zcount zset 2 3: Returns the number of intervals in the collection > Zcard zset: Returns the elements in the collection Number > Zscore Zset one: Returns the one element of score > Zremrangebyrank zset 3 3: Deletes the element in the collection in the given interval > zremrangebyscore zset 1 2: Will zset in Score from small to large sorted results between 1-2 deletions
Hash (hashed)
A Redis hash is a collection of key-value pairs. Redis Hash is a string-type field and value mapping table, and hash is particularly useful for storing objects.
Example
127.0.0.1:6379> hmset user:1 username runoob password Runoob points 200ok127.0.0.1:6379> hgetall user:11) "username "2)" Runoob "3)" password "4)" Runoob "5)" points "6)" 200 "the hash data type in the above instance stores the user object containing user script information. In the example we used Redis hmset, Hgetall command, user:1 as the key value. Each hash can store 232-1 key-value pairs (more than 4 billion). ------------------------------------------hset Command: Set the key and value of a hash table  >hset hash name key  value  such as: >hset user:001 Name Zhangsan  hget command: Gets the corresponding value of the key in the execution hash name?  HSETNX command: Set a hash table that does not exist in the key and value  >hsetnx hash name key  value  //successfully returned 1, failed to return 0  such as: >hsetnx user:001 name Zhangsan?  Hmset command: Hmset user:001 username Zhangsan age sex 1 Batch setup Hmget user:001 username age  Sex: Get values in bulk?  >hexists user:001 name//exists if there is a return of 1?  >hlen user:001  //Get the number of keys in a hash user001 name?  >hdel user:001 name//delete hash user:001 in the name key?  >hkeys user:002//Returns all keys in the hash named user:002.  >hvals user:002//Returns all values in the hash named user:002.  >hgetall user:002//Returns all keys and values in the hash named user:002.

Redis Advanced Utility featuresadd a password for Redis
Warning: Because Redis is very fast, an external user can make a 150K password attempt in a second on a better server, which means you need to specify a very strong password to prevent brute force. 1. Enter the configuration file: Vi/usr/local/redis /etc/redis.conf settings: Requirepass redis password 2. Restart Service: #./REDIS-CLI shutdown execution Close #./redis-server/usr/local/redis/etc/redis.conf  start 3. Login (two) #./REDIS-CLI Client command link server >auth password value  //authorization can use? #./redis-cli-a  Password//password to authorize when connecting

Brothers Learn python---redis basics

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.