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?
- [[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 #<span style= "text-indent: 20px" > Assumptions php Directory: </span><span style= " text-indent: 20px ">/USR/LOCAL/PHP</SPAN>&NBSP;&NBSP;
- [[email protected] phpredis]# ./configure --with-php-config=/usr/local/php/bin/php-config
- [[EMAIL&NBSP;PROTECTED]&NBSP;PHPREDIS]#&NBSP;MAKE&NBSP;&NBSP;
- [[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?
- 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?
- [Redis]
- 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?
- <?php
- $redis = new Redis (); #实例化redis类
- $redis->connect (' 127.0.0.1 '); #连接服务器
- $redis->set (' key ', ' hello '); #调用方法, set String type value
- $redis->append (' key ', ' world '); #修改string类型值
- echo $redis->get (' key '); #获取redis the value of key and outputs the display
- echo $redis->type (' key '); #获取key data type
- echo $redis-Echo (' would close ... '); # output string
- $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