This article mainly introduces the implementation of the basic REDIS data structure of PHP, has a certain reference value, now share to everyone, the need for friends can refer to
Redis basic data structure and PHP implementation
-
Redis (REmote DIctionary Server) is an open source API that is written in ANSI C, adheres to the BSD protocol, supports networks, can be persisted in memory, key-value databases, and provides multiple languages.
-
Redis is commonly referred to as a data structure server because values (value) can be strings (string), hashes (MAP), lists (list), collections (set), and ordered collections (sorted sets) and other types
Redis Configuration and Connectivity
Redis.phpreturn [' Host ' = ' 127.0.0.1 ', ' port ' = ' 6379 '];//Redistest.php$redis = new Redis (); $redisConf = Includ E ' redis.php '; $redis->connect ($redisConf [' Host '], $redisConf [' Port '];
Redis Key (Key)
Redis key Operation $redis->exists ($key); Determine if the key value exists $redis->expire ($key, 10); Set key to expire after 10 seconds
Redis string (String)
Redis string String $redis->set ($key, $val); $redis->incr ($key); The key value is +1 unless Val is an integer, otherwise the function fails $REDIS->DECR ($key); Key value-1, ditto $redis->append ($key, "UE"); Append key value Content $redis->strlen ($key); Returns the length of the key value//When the key value is set for the first time, the data type of the key value cannot be changed. $redis->del ($key); Delete key value
Redis Hash (hash)
-
Redis Hash is a string-type field and value mapping table, and hash is particularly suitable for storing objects
-
Each hash in a Redis can store 2^ (32)-1 (more than 4 billion) key-value pairs
Redis Hash Hash $redis->hset ($key, ' field1 ', ' val1 '); Set a Key-value key value pair $redis->hmset ($key, Array (' field2 ' = ' val2 ', ' field3 ' = ' val3 ')); Set multiple K-v key values to $redis->hget ($key, ' field2 '); Get a hash of one of the key values $redis->hmget ($key, Array (' Field2 ', ' field1 ')); Gets the hash of multiple key values $redis->hgetall ($key); Gets all the key values in the hash $redis->hlen ($key); Gets the number of key-value pairs in the hash $redis->hkeys ($key); Gets all the key $redis->hvals ($key) in the hash; Get all the values in the hash
Redis lists (list)
-
The Redis list is a simple list of strings, sorted by insert order , you can add a list of elements to the head (left) or tail (right)
-
A list in Redis can store up to 2^ (32)-1 elements
Redis list $index = $start = 0; $redis->lpush ($key, ' val1 ', ' val2 '); Add multiple values at the beginning of the list $redis->lpop ($key); Removes and obtains the first element of list $redis->rpop ($key); Remove and get the last element of the list $stop = $redis->llen ($key)-1; Gets the length of the list $redis->lindex ($key, $index); Gets the list element $redis->lrange by index ($key, $start, $stop); Gets the element within the specified range
Redis Collection (SET)
-
Redis's set is an unordered collection of type string. A collection member is unique, which means that duplicate data cannot appear in the collection
-
Collections in Redis are implemented by hash tables, so the complexity of adding, deleting, and finding is O (1)
-
Up to 2^ (32)-1 members in a collection in Redis
Redis Set unordered set $redis->sadd ($key, ' val1 ', ' val2 '); Adds multiple elements to the collection $redis->scard ($key); Gets the number of collection elements $redis->spop ($key); Remove and get a random element within the set $redis->srem ($key, ' val1 ', ' val2 '); Removes multiple elements of a collection $redis->sismember ($key, ' val1 '); Determines whether an element exists within the collection
Redis ordered set (sorted set)
-
Redis ordered collections and collections are also collections of elements of type string and do not allow duplicate members
-
The difference is that each element is associated with a double-type fraction . Redis is a small-to-large ordering of the members in a collection by fractions
-
The members of an ordered collection are unique, but fractions (score) can be repeated
-
The collection is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1). The maximum number of members in the collection is 2^ (32)-1
The elements in the Redis sorted set ordered set//ordered set are associated with a fractional score, which is score sorted by this score $redis->zadd ($key, $score 1, $val 1, $score 2, $val 2 ); Adds multiple elements to the collection $redis->zcard ($key); Gets the total number of elements in the collection $redis->zcount ($key, $minScore, $maxScore); Gets the element $redis->zrem within the category range of the collection ($key, $member 1, $member 2); Remove multiple elements within a collection
Redis Hyperloglog
-
Redis Hyperloglog is an algorithm used to do cardinality statistics ( counting the number of distinct elements in a dataset), and the advantage of Hyperloglog is that when the number of input elements or the volume is very large, the space required to calculate the cardinality is always fixed and small
-
In Redis, each Hyperloglog key requires only a few kilobytes of memory to calculate the cardinality of a different element close to 2^ (64). This is in stark contrast to the larger set of elements that consume more memory when calculating cardinality.
-
Because Hyperloglog only calculates the cardinality based on the input element and does not store the INPUT element itself, Hyperloglog cannot return individual elements of the input as a collection.
$redis->pfadd (' Key1 ', Array (' elem1 ', ' elem2 ')),//Add the specified element to Hyperloglog (' $redis->pfadd ', Array (' Key2 ', ' Elem2 ');//merge multiple hyperloglog into one hyperloglog$redis->pfmerge (' Key3 ', Array (' Key1 ', ' Key2 ')); $redis->pfcount (' Key3 '); Returns the cardinality estimate for Hyperloglog: Int (3)
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!