Redis Overview
Redis is an open-source, Advanced Key-value store. It is commonly referred to as a data structure server because keys can contain strings, hashes, linked lists, collections and an ordered set of Redis briefs Redis is completely open source free, complies with BSD protocol and is a high performance Key-value database. Redis and other Key-value cache products have the following three features: Redis support data persistence, you can save in-memory data on disk, restart can be loaded again for use. Redis not only supports simple key-value types of data, but also provides storage of data structures such as List,set,zset,hash. Redis supports backup of data, that is, Master-slave mode of data backup. (Master-slave) Redis Advantage performance is very high –redis can read the speed is 110,000 times/s, the write speed is 81,000 times/s. Rich data types –redis support for binary case Strings, Lists, hashes, sets and Ordered sets all operations of atomic –redis are atomic, and Redis supports atomic execution of several operations. Rich features –redis also supports publish/subscribe, notifications, key expiration, and more. What is the difference between Redis and other Key-value storage? Redis has more complex data structures and provides atomic manipulation of them, which is a different evolutionary path from other databases. The data types of Redis are based on basic data structures and are transparent to programmers without the need for additional abstraction. Redis runs in memory but can be persisted to disk, so it is necessary to weigh the memory when writing to different datasets at high speed because the amount of data cannot be larger than the hardware memory. Another advantage of the in-memory database is that it is very simple to operate in memory compared to the same complex data structure on disk, so Redis can do a lot of things that are internally complex. At the same time, they are compact in the form of disks, because they do not have to be randomly accessed.
Redis Installation
Redis installation is divided into Linux under the installation and win under the installation, network tutorials More, here non-emphasis omitted!
Redis data types
Redis supports five types of data: string (String) list (list) set (collection) (unordered collection) Zset (ordered collection) 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.
String operation:
1.set Key value //Set a key and value that does not exist create, overwrite, return ok2.get key //Get value of a key, return result 3.setnx key value //Set a non-existent key and value 4.setex key seconds value //Set a key value with expiration date 5.ttl key //view key for valid time 6.setrange key Position value //replacement string EX: setrange name 4 strs // Replace the name key with the fourth position. Strs7.mset key1 value1 key2 value2 key3 value3 //Batch Set key value 8.msetnx // Bulk set non-existent key value pair 9.getset //Get original value replaced with other value EX: getset key new_value10.getrange //Gets the value of the specified range EX: getrange key 0 411.mget //Batch Get value 12.INCR //Gagacao as 13.incrby key 2 //Set a key value plus a certain number (both positive and negative) 14.DECR //minus minus operation 15.decrby key 2 //Set a key value minus a number 16.append key app_value// append value to specified key 17.strlen key //Length of key
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).
List operations
1. Lpush list1 ' Hello ' //in List1 head Insert a string 2.rpush list1 ' world '/ /Insert a string at List1 tail 3.lrange list1 0-1 //Get all content in List1 4.linsert list1 "Before/after" Old_value new_value // Insert a new value before or after the old value 5.lset list1 0 ' hello ' //Modify the value at the specified position 6.lrem list1 2 ' Hello ' //Delete the first two Hello values 7.lrem list1 -2 ' hello ' //delete after two hello values 8.lrem list1 0 ' Hello ' //delete all Hello values 9.ltrim list1 1 3 //Delete values outside the specified range 10.lpop list1 // Removes the element from the List1 's head and returns 11.rpop List1// removes the element from the tail of List1 and returns 12.lindex list1 1 // Returns the element at index position in List1 13.llen list1 //Returns the length on the List1
Unordered collection (set)
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
Unordered collection Operations
1.sadd key value sadd myset ' Hello ' //To collection no set adds a data 2.smembers myset // Get all elements in MySet 3.srem myset ' one ' //delete elements in MySet one4.spop myset // Randomly deletes an element in the collection and returns 5.srandmember myset //Random return value in MySet without deleting 6.smove myset1 myset2 Lala Move lala in Myset1 to Myset2 7.scard myset1 //return myset1 worth 8.sismember myset lala // Determine if Lala is in MySet 9.sdiff myset1 myset2 //Returns the difference set in two sets 10.sdiffstore newset Myset1 Myset2 //Save the difference set of two sets to Newset 11.sinter myset1 myset2 //return intersection of two sets 12.sinterstore Newset myset1 myset213.sunion myset1 myset2 //return set of two sets 14.sunionstore newset Myset1 Myset2
Ordered set (Zset)
Ordered collection operations
1.zadd zset 1 One //Add one to Zset, sort the weights Score2.zrem zset One /remove elements from Zset One3.zincrby Zset 2 One //if present One,score plus 2, not present add as 24.zrank Zset one //return one in Zset ranking (small to large) 5.zrevrank Zset One //return one in Zset ranking (from big to small) 6.zrangebyscore zset 2 3 withscores // Returns the element in the set score at a given interval (closed interval) 7.zcount zset 2 3 //Returns the specified interval worth the number 8.zcard zset // Returns the number of elements in the collection 9.zremrangebyrank zset 3 3 //delete the element in the collection in the specified interval 10.zremrangebyscore zset 1 2 //Remove elements from small to large score in the collection between
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.
Hash operation
1.hset hashname key value //Set a hash table of key values 2.hsetnx hashname key value // Set a hash table that does not exist in the key and the value 3.hmset hashname key1 value1 key2 value2 ... Batch setup 4.hmget hashnane key1 key2 ... Bulk get 5.hexists hashname key //Detect if key is present 6.hlen hashname //Get the number of keys in the hash table 7.hdel Hashname key //delete hash table in Key8.hkeys hashname //return hash table for all keys 9.hvals hashname // Returns all values in the hash table 10.hgetall hashname //Returns all keys and values
Redis Advanced Applications
1. Security Requirepass Password
2. Persistent AppendOnly Yes
Modifying a configuration file vim/etc/redis/redis.conf
Redis Database Learning