Redis series-php How to use Redis with Redis extensions

Source: Internet
Author: User
Tags pconnect vars zts redis server

from:http://blog.csdn.net/love__coder/article/details/8691679

Through the previous several blog, we should have a general understanding of Redis, here again, how to connect the Redis server to read and write data?

1. Install PHP extensions

A) Install PHP extension Phpredis:

[Plain]View Plaincopyprint?
    1. [[email protected] tool]# git clone https://github.com/nicolasff/ phpredis.git phpredis  
    2. [[email protected] tool]# cd phpredis/   
    3. [[email protected] phpredis]# /usr/local/php/bin/phpize    #<span style= "text-indent: 20px" > Assumptions  php Directory: </span><span style= " text-indent: 20px ">/USR/LOCAL/PHP</SPAN>&NBSP;&NBSP;
    4. [[email protected]  phpredis]# ./configure --with-php-config=/usr/local/php/bin/php-config  
    5. [[EMAIL&NBSP;PROTECTED]&NBSP;PHPREDIS]#&NBSP;MAKE&NBSP;&NBSP;
    6. [[email protected] phpredis]# make install  
[[email protected] tool]# git clone https://github.com/nicolasff/phpredis.git phpredis[[email protected] tool]# CD Phpredis/[[email protected] phpredis]#/usr/local/php/bin/phpize   #假设 php Directory:/usr/local/php[[email protected] phpredis]#./configure--with-php-config=/usr/local/php/bin/php-config[[email protected] phpredis]# Make[[email Protected] phpredis]# make install
If it goes well, you'll get a hint like this:

[Plain]View Plaincopyprint?
    1. 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 the php.ini file, add the configuration:

[Plain]View Plaincopyprint?
    1. [Redis]
    2. Extension=redis.so
[redis]extension=redis.so

Restart the Web server, Echo phpinfo (), and you will see a general display like this:

2. Access to Redis server by extension

How does PHP use Redis server to access data? It is only through the Phpredis extension module, through the following several steps:

1. Instantiate Redis class "New Redis ()"

2, Call Redis class method, connect Redis server "Connect,pconnect"

3. Set connection option "SetOption"

4. Save/Fetch data

5. Close the connection "close"

If you use the default option, step 3 is not required. See the following code example:

[PHP]View Plaincopyprint?
  1. <?php
  2. $redis = new Redis (); #实例化redis类
  3. $redis->connect (' 127.0.0.1 '); #连接服务器
  4. $redis->set (' key ', ' hello '); #调用方法, set String type value
  5. $redis->append (' key ', ' world '); #修改string类型值
  6. echo $redis->get (' key '); #获取redis the value of key and outputs the display
  7. echo $redis->type (' key '); #获取key data type
  8. echo $redis-Echo (' would close ... '); # output string
  9. $redis->close (); #关闭连接
<?php$redis = new Redis (); #实例化redis类 $redis->connect (' 127.0.0.1 '); #连接服务器 $redis->set (' key ', ' hello '); #调用方法, set the string type value $redis->append (' Key ', ' world '); #修改string类型值echo $redis->get (' key ');  #获取redis the value of key, and the output shows echo $redis->type (' key '); #获取key data type echo $redis->echo (' would close ... '); # output string $redis->close (); #关闭连接
With the code above, we basically complete a simple Redis access operation. Some of the properties and methods of the Redis class are listed below

A) Connect to Redis server:

    • Connect: Connecting to server
    • Pconnect: Long Connection
    • AUTH: Permission Validation
    • Select: Choose DB
    • Close: Closing the connection
    • SetOption: Set client options
    • GetOption: Get Client options
    • ping:ping Redis Server
    • Echo: Output string

Note that if you operate redis frequently, and the connect and close will be very expensive, it is recommended to use Pconnect to establish a long connection.

b) string read and write function

    • Append: Append a value after a value
    • DECR: decrements the value of a key
    • INCR: Increments the value of a key
    • Get: Get a value
    • Set: Sets a value
    • Getset: Sets the value and returns the old value
    • MGet: Bulk Fetch values
    • MSet: Batch Setting values
    • Strlen: Get value length

Note: If you can use bulk operations as much as possible, reduce the frequent connection to Redis database performance

c) hash read and write function

    • Hdel: Delete a multiple domain
    • Hexists: Determine if a hash field exists
    • Hget: Gets the value of the hash field
    • Hgetall: Get all domain values
    • Hincrby: Self-growing value of a hash int field
    • Hkeys: Get hash all fields
    • Hlen: Get the number of domains
    • Hmget: Bulk Get the value of a domain
    • Hmset: Bulk set values for a field
    • Hset: Setting the value of a field
    • Hvals: Get values for all domains

d) List read and write function

    • Linsert: inserting elements
    • Llen:list length
    • Lpop: Remove and get the first color
    • Lpush: Inserting an element
    • Lrem: removing elements
    • LSet: Setting element values

E) Set

    • Sadd: Add one or more members
    • Sismember: Whether it contains
    • Smembers: Get member
    • Smove: Moving Members
    • SPop: removing members
    • Srandmember: Get random Members
    • Srem: Delete

f) Sorted set

    • Zadd: Add one or more
    • Zcard: Number of Members
    • Zincrby: Incrementing member score
    • Zrange: Returns a member within an index range
    • Zrangebyscore: Returns members within the score range
    • Zscore: Get member Score
    • Zrem: Remove one or more members

For more information, please refer to: Https://github.com/nicolasff/phpredis

Http://redis.io/clients

Redis series-php How to use Redis with Redis extensions

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.