Redis series-php how to use redis extension through redis

Source: Internet
Author: User

1. install php extension a) install php extension phpredis: [plain] [root @ xsf002 tool] # git clone https://github.com/nicolasff/phpredis.git phpredis [root @ xsf002 tool] # cd phpredis/[root @ xsf002 phpredis] #/usr/local/php/bin/ phpize # <SPAN style = "TEXT-INDENT: 20px "> suppose php Directory: </SPAN> <SPAN style =" TEXT-INDENT: 20px ">/usr/local/php </SPAN> [root @ xsf002 phpredis] #. /configure -- with-php-config =/usr/local/php/bin/php-config [root @ xsf002 Phpredis] # make [root @ xsf002 phpredis] # make install [root @ xsf002 tool] # git clone https://github.com/nicolasff/phpredis.git phpredis [root @ xsf002 tool] # cd phpredis/[root @ xsf002 phpredis] #/usr/local/php/bin/phpize # assume the php Directory: /usr/local/php [root @ xsf002 phpredis] #. /configure -- with-php-config =/usr/local/php/bin/php-config [root @ xsf002 phpredis] # make [root @ xsf002 phpredis] # make install if smooth, the following is a reference. Description: [plain] Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/php extension file directory, there will be a redis. so file. B) Load redis. so in php. in the ini file, add the configuration: [plain] [redis] extension = redis. so [redis] extension = redis. so restart the web server, echo phpinfo (), will see roughly as follows: 2. How to access the redis server through extended access php using the redis server to access data? It is nothing more than the phpredis extension module, through the following steps: 1. instantiate the redis class [new redis ()] 2. Call the redis class method to connect to the redis server [connect, pconnect: 3. Set the connection option setOption. 4. Store/retrieve data. 5. close the connection. If the default option is used, skip step 3. See the following code example: [php] <? Php $ redis = new Redis (); # instantiate redis class $ redis-> connect ('2017. 127. 0.0.1 '); # connect to the server $ redis-> set ('key', 'Hello'); # Call the method and set the string type value $ redis-> append ('key ', 'World'); # modify the string type value echo $ redis-> get ('key'); # obtain the redis key value, echo $ redis-> type ('key'); # obtain the data type of the key echo $ redis-> echo ('will close... '); # output string $ redis-> close (); # close the connection <? Php $ redis = new Redis (); # instantiate redis class $ redis-> connect ('2017. 127. 0.0.1 '); # connect to the server $ redis-> set ('key', 'Hello'); # Call the method and set the string type value $ redis-> append ('key ', 'World'); # modify the string type value echo $ redis-> get ('key'); # obtain the redis key value, echo $ redis-> type ('key'); # obtain the data type of the key echo $ redis-> echo ('will close... '); # output string $ redis-> close (); # close the connection through the above Code, we basically complete a simple redis access operation. Some attributes and methods of Redis are listed below. a) connect to redis server: connect to serverpconnect: persistent connection auth: Permission verification select: select DBclose: close connection setOption: set the client Option getOption: Get the client Option ping: ping redis server echo: Output string Note: If you operate redis frequently, non-stop connect and close will consume a lot of performance. At this time, we recommend that you use pconnect to establish a persistent connection B) string read/write function append: append value after the value decr: decrease the value of a key incr: Increase the value of a key get: get a value set: set a value getSet: set a value, and return the old value mGet: Batch get value mSet: Batch set value strlen: Get value length note: if you can use batch operations, try to use batch operations to reduce frequent connection to redis database performance c) hash read/write function hDel: delete multiple domains hExists: Determine whether a hash domain has hGet: get the value of the hash field hGetAll: Get the value of all domains hIncrBy: Increase the value of a hash int field hKeys: Get the hLen of all hash domains: Get the number of domains hmet: obtain the value of the domain in batches hMSet: batch set the value of the field hSet: set the value of the field hVals: Get the value of all fields d) list read/write function lInsert: insert element lLen: list length lPop: remove and obtain the first color lPush: insert an element lRem: Remove element lSet: set element value e) set sAdd: add one or more Members sIsMember: whether it contains sMembers: get member sMove: Move member sPop: Remove member sRandMember: Get random member sRem: delete f) sorted set zAdd: add one or more zcards: Number of Members zIncrBy: incrementing member scorezRange: returns zRangeByScore, a member in the index range. Returns zScore, a member in the score range. obtains scorezRem. removes one or more members.

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.