Home  >  Article  >  Backend Development  >  Redis series-php how to use redis through redis extension_PHP tutorial

Redis series-php how to use redis through redis extension_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:09:38828browse

 
 
 
 
1、安装php扩展
 
a)安装php扩展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   #假设 php目录:/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  
 
[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   #假设 php目录:/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 如果顺利,将得到类似如下提示: 
 
 
[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扩展文件目录下,将有个redis.so文件。 
 
b)加载redis.so
 
在php.ini 文件中,添加配置:
 
 
[plain] 
[redis]  
extension=redis.so  
 
[redis]
extension=redis.so重启web服务器,echo phpinfo() ,将看到大致如下显示:
 
 
 
 2、通过扩展访问redis服务器
 
php如何使用redis服务器存取数据?无非是经过phpredis扩展模块,通过如下几个步骤:
 
1、实例化redis类   【new redis()】
 
2、调用redis类方法,连接redis服务器 【connect,pconnect】
 
3、设置连接选项【setOption】
 
4、存/取数据 
 
5、关闭连接 【close】
 
如果使用默认选项,步骤3不需要。请参看下面的代码示例:  
 
 
[php] 
$redis = new Redis(); #实例化redis类  
$redis->connect('127.0.0.1'); #连接服务器  
$redis->set('key', 'hello '); #调用方法,设置string类型值  
$redis->append('key', 'world'); #修改string类型值  
echo $redis->get('key');  #获取redis key的值,并输出显示   
echo $redis->type('key'); #获取key 的数据类型  
echo $redis->echo('will close...');# 输出字符串  
$redis->close(); #关闭连接  
 
$redis = new Redis(); #实例化redis类
$redis->connect('127.0.0.1'); #连接服务器
$redis->set('key', 'hello '); #调用方法,设置string类型值
$redis->append('key', 'world'); #修改string类型值
echo $redis->get('key');  #获取redis key的值,并输出显示 
echo $redis->type('key'); #获取key 的数据类型
echo $redis->echo('will close...');# 输出字符串
$redis->close(); #关闭连接  通过上面的代码,我们基本完成一个简单redis的存取操作。下面罗列一些Redis类的一些属性及方法 
 
a)连接redis server:
 
 
connect :连接server
pconnect :长连接
auth :权限验证
select :选择DB
close : 关闭连接
  setOption : 设置 client 选项
getOption : 获取client选项
ping : ping redis server
 echo : 输出 字符串
 
Note that if you operate redis frequently, continuous connect and close will consume a lot of performance. At this time, it is recommended to use pconnect to establish a long connection
b) String reading and writing functions
append: Append the value after the value
decr: Decrement the value of a key
incr: increment the value of a key
get: Get a value
set: Set a value
getSet: Set the value and return the old value
mGet: Get values ​​in batches
mSet: Set values ​​in batches
strlen: Get the value length
Note: If you can use batch operations, try to use batch operations to reduce the performance of frequent connections to the redis database
c) hash reading and writing function
hDel: Delete multiple domains
hExists: Determine whether a hash domain exists
hGet: Get the value of hash field
hGetAll: Get all domain values
hIncrBy: auto-increment the value of a hash int field
hKeys: Get hash of all domains
hLen: Get the number of domains
hMGet: Get domain values ​​in batches
hMSet: Set domain values ​​in batches
hSet: Set the value of the field
hVals: Get the values ​​of all fields
d) list reading and writing functions
lInsert: Insert element
lLen: list length
lPop: Remove and get the first color
lPush: Insert an element
lRem: remove element
lSet: Set element value
e)set
sAdd: Add one or more members
sIsMember: Whether to contain
sMembers: Get members
sMove: Move members
sPop: Remove members
sRandMember: Get a random member
sRem: Delete
f)sorted set
zAdd: Add one or more
zCard: Number of members
zIncrBy: Increment member score
zRange: Returns members within the index range
zRangeByScore: Returns members within the score range
zScore: Get member score
zRem: Remove one or more members

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477640.htmlTechArticle1. 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@x...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn