In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Redis is a Cramp S structure service, C refers to the client, S refers to the server, the client and the server can communicate through the network. For redis, the redis service needs to be installed on the server, what about the client? In fact, redis provides API for many languages, which can communicate between client and server through language. for php language, we can realize the communication between client and server by installing redis extension.
String type operation
String is the most basic type of redis, and the string type is binary safe. It means that the string of redis can contain any data. Such as jpg pictures or serialized objects
$redis- > set ('key','TK'); $redis- > set (' number','1'); $redis- > setex ('key',5,'TK'); / / set key value $redis- > psetex (' key',5000,'TK') valid for 5 seconds; / / set key value $redis- > setnx ('key','XK') valid for 5000 milliseconds (same as 5 seconds); / / return true$redis- > delete (' key') if false does not exist Delete key values can be passed into the array array ('key1','key2') to delete multiple keys $redis- > getSet (' key','XK') / / set the value of the key key to XK and return the original value of the key TK $ret = $redis- > multi () / / batch transaction, which does not guarantee the atomicity of processing data-> set ('key1',' val1')-> get ('key1')-> setnx (' key', 'val2')-> get (' key2')-> exec (); $redis- > watch ('key') / / Monitor whether the key key has been modified by other clients. If KEY is modified between calling watch () and exec (), exec fails $redis- > publish ('chan-1',' hello, worldview'); / / send message. $redis- > exists ('key'); / / verify whether the key exists, return true$redis- > incr (' number'); / / key value plus 1$ redis- > incrby ('number',-10); / / key value plus or minus 10$ redis- > incrByFloat (' number', + /-1.5); / / key value plus or minus decimal $redis- > decr ('number'); / / key value minus 1$ redis- > decrBy (' number',10) / / the key value minus 10$ mget = $redis- > mget (array ('number','key')); / / get the key value in batch and return an array of $redis- > mset (array (' key0' = > 'value0',' key1' = > 'value1')); / / set the key value in batch $redis- > msetnx (array (' key0' = > 'value0',' key1' = > 'value1') / / set key values in batches, similar to batch operation of the setnx () method $redis- > append ('key','-Smudge'); / / append the original key value TK to the key value, with the key value TK-Smudge$redis- > getRange ('key', 0,5); / / key value interception starts at position 0 and ends at position 5$ redis- > getRange (' key',-6,-1) / / string interception starts from-6 (penultimate position) to-1 (penultimate position) ends with $redis- > setRange ('key', 0,' Smudge'); / / replace the string in the key value, where 0 indicates how many characters have been replaced from position 0, in which Chinese characters occupy 2 positions $redis- > strlen ('key'); / / key length $redis- > getBit (' key') $redis- > setBit ('key')
List linked list operation
$redis- > delete ('list-key'); / delete linked list $redis- > lPush (' list-key','A'); / / insert header / left of linked list, return list length $redis- > rPush ('list-key',' B'); / / insert tail / right of linked list, return list length $redis- > lPushx ('list-key',' C') / / insert the head / left side of the linked list. If the list does not exist, it will return 0. If it exists, it will be inserted successfully. The current length of the list $redis- > rPushx ('list-key',' C') will be returned. / / the tail / right side of the list will be inserted. If the list does not exist, 0 will be returned, and the length of the current list $redis- > lPop ('list-key') will be returned. / / return the VALUE at the top (left) of the LIST, then $redis- > rPop ('list-key') in the stack; / / return the VALUE at the end of the LIST (on the right), $redis- > blPop (queue); $redis- > brPop (); $redis- > lSize (' list-key') / / if it is a linked list, the length of the linked list is returned. If the empty linked list returns 0, if it is not a linked list or is not empty, return false and judge the non-linked list "= false" $redis- > lGet ('list-key',-1); / / get the element 0 of the linked list through the index, get a-1 on the left and get the last $redis- > lSet (' list-key', 0,'X'). / / replace the position element with redis--> lRange ('list-key', 0,3); / / list interception starts at 0 and ends at position 3, ending at-1 to get all the $redis- > lTrim (' list-key', 0,1) after the start position; / / intercepting the linked list (irreversible) starts at 0 and ends with index 1 ending $redis- > lRem ('list-key',' Category, 2) / / the linked list deletes the element from the left 2 Centrals-> lInsert ('list-key', Redis::BEFORE,' inserted,'X'); / / inserts X before the C element, Redis::AfTER (indicates later insert) if the linked list does not exist, the insert fails to return 0 if the element does not exist return-1$ redis- > rpoplpush ('list-key',' list-key2') / / Pop an element from the end of the source LIST and press this element into the target LIST from the top (left) of the target LIST. The blocking version of $redis- > brpoplpush (); / / rpoplpush, which has a third parameter to set the blocking time, that is, if the source LIST is empty, you can block the listening time of the timeout, and perform the operation if there is an element.
Set collection type
Set unordered collections do not allow duplicate elements the server can implement multiple collection operations $redis- > sMembers ('key'); / / get all the elements in the container key $redis- > sAdd (' key', 'TK') / / (insert from the left, and the last inserted element is at position 0). If TK already exists in the collection, return false. If added successfully, return true$redis- > sRem ('key','TK'); / / remove TK$redis- > sMove (' key','key1','TK') from container; / / move element TK in easy key to container key1 successfully return TRUE$redis- > sIsMember ('key','TK') / / check whether VALUE is a member of the SET container $redis- > sCard ('key'); / / return the number of members of the SET container $redis- > sPop (' key'); / / randomly return an element in the container and remove the element $redis- > sRandMember ('key'); / / randomly return an element in the container without removing the element $redis- > sInter (' key','key1') / / returns the intersection of two sets without intersection returns an empty array. If the parameter has only one collection, the complete array $redis- > sInterStore ('store','key','key1') corresponding to the collection is returned; / / the intersection of set key and collection key1 is stored in container store successfully returning 1$ redis- > sUnion (' key','key1') / / Union of collection key and collection key1 Note that even if multiple collections have the same elements, only one $redis- > sUnionStore ('store','key','key1') is retained; / / the union of collection key and collection key1 is saved in the collection store, note that even if multiple collections have the same elements, only one $redis- > sDiff (' key','key1','key2') is retained / / returns an array that exists in the key collection but not in the collection key1 key2
Zset data type
* * (stored set) is a collection of strings like set, except that each element is associated with a score of type double
The list type of redis is actually a two-way linked list with each child element of type string. **
$redis- > zAdd ('tkey', 1,' A'); / / insert into the collection tkey. Element An is associated with a score. If the insertion is successful, the collection element cannot be repeated. If the element already exists, return 0$ redis- > zRange ('tkey',0,-1); / / get the collection element, from position 0 to position-1$ redis- > zRange (' tkey',0,-1, true) / / gets the collection element and returns an associative array with scores array from position 0 to position-1 ([A] = > 0.01,[ B] = > 0.02, [D] = > 0.03), where the decimal comes from the second parameter of the zAdd method, $redis- > zDelete ('tkey',' B'). / / remove element B from the collection tkey successfully return 1 failed return 0$ redis- > zRevRange ('tkey', 0,-1); / / get the collection element, from position 0 to position-1, the array processes $redis- > zRevRange according to score descending order (' tkey', 0,-1 true) / / get the collection elements. From position 0 to position-1, the array returns the score associative array $redis- > zRangeByScore ('tkey', 0,0.2 tkey', array (' withscores' = > true) in descending order of score). / / get several tkey elements whose score is in the interval [0jue 0.2], the score is sorted from low to high, and the elements have the same score, then they will be arranged in dictionary order. Withscores control returns the associative array $redis- > zRangeByScore ('tkey', 0.1,0.36, array (' withscores' = > TRUE, 'limit' = > array (0,1). / where 0 and 1 in limit means to start at position 0 in the eligible set, scan backward one to return the associative array $redis- > zCount ('tkey', 2,10); / / get the number of score elements in interval [2,10] in tkey $redis- > zRemRangeByScore (' tkey', 1,3); / / remove the element $redis- > zRemRangeByRank ('tkey', 0,1) of score in interval [1,3] (including boundary) in tkey / / the default element score is incremented. Remove the element from tkey from 0 to-1 and end $redis- > zSize ('tkey'); / / return the number of elements stored in the ordered set corresponding to key $redis- > zScore (' tkey','A'); / / return the score value of element An in the set tkey $redis- > zRank ('tkey',' A') / / returns the index value of element An in the collection tkey z the elements in the collection are arranged according to score from lowest to highest, that is, the lowest score index index is 0$ redis- > zIncrBy ('tkey', 2.5,' A'); / / add the score value of element An in the collection tkey by 2.50$ redis- > zUnion ('union', array (' tkey', 'tkey1')) / / merge the collection tkey and collection tkey1 elements into the collection union, and the elements in the new collection cannot repeatedly return the number of elements in the new collection. If element An exists in both tkey and tkey1, the score of the merged element An adds $redis- > zUnion ('ko2', array (' K1, 'k2'), array (5, 2)) / the set K1 and the set K2 are merged in K02, and the number of elements in array corresponds to the subset, and then 5 corresponds to K1 K1 each element score is multiplied by 5. In the same way, 1 corresponds to K2LI K2 each element score times 1, and then the elements are sorted incrementally. By default, the same elements score (SUM) add $redis- > zUnion ('ko2', array (' K1,'K2), array (10, 2), 'MAX') / / after each subset is multiplied by a factor, the elements are sorted incrementally. For the same element, the score takes the maximum value (MAX). You can also set the MIN to take the minimum value $redis- > zInter ('ko1', array (' K1, 'K2')). / / set K1 and set K2 intersect at K01, and sort incrementally according to the score value. If the collection elements are the same, the score values of the elements in the new collection add $redis- > zInter ('ko1', array (' K1, 'K2'), array (5, 1)) / the set K1 and the set K2 intersect at K01, and the number of elements in array corresponds to the subset, then 5 corresponds to K1 K1 each element score is multiplied by 5, similarly 1 corresponds to K2 K2 each element score times 1, and then the element score is sorted incrementally. By default, the same elements score (SUM) add $redis- > zInter ('ko1', array (' K1, 'K2'), array (5L1),' MAX') / / after each subset is multiplied by a factor, the element score is sorted incrementally, and the same element score takes the maximum value (MAX). You can also set MIN to take the minimum value.
Hash data type
Redis hash is a mapping table of field and value of type string. Its add and delete operations are both O (1) (average). Hash is especially suitable for storing objects.
$redis- > hSet ('hashes,' name', 'TK'); / / add name field value to h table as TK$redis- > hSetNx (' hackers, 'name',' TK'); / / add name field value to TK in h table if value of field name returns false otherwise return true$redis- > hGet ('hashes,' name'); / / get name field value$redis- > hLen ('h') in h table / / get the length of the h table, that is, the number of fields $redis- > hDel ('hobbies hDel'); / / delete the email field $redis- > hKeys ('h') in the h table; / / get all the fields in the h table $redis- > hVals ('h'); / / get all the fields in the h table value$redis- > hGetAll ('h') / / get all the fields in the h table and value return an associative array (fields are key values) $redis- > hExists ('hashes,' email'); / / determine whether the email field exists and table h does not exist return false$redis- > hSet ('hashes,' age', 28); $redis- > hIncrBy ('hashes,' age',-2) / / set the age field value plus (- 2) in the h table. Return false if value is a non-numeric value. Otherwise, return value$redis- > hIncrByFloat ('hashes,' age',-0.33) after operation. / set age fields value plus (- 2.6in h table) return false if value is a non-numeric value otherwise return value after operation (15 decimal places) $redis- > hMset ('hashes, array (' score' = > '80th,' salary' = > 2000)); / / Table h batch set fields and value$redis- > hMGet ('hashes, array (' score','salary')); / Table h batch get value of fields
Summary
The above is the syntax commonly used in redis in php introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.