PHP链接redis

在php程序中使用Redis之前,需要确保在机器上安装了Redis的PHP驱动程序和PHP环境。可以先在将PHP电脑上并配置好环境。

安装

现在,让我们看看如何设置Redis PHP驱动程序。
从github库下载phpredis=> http://github.com/nicolasff/phpredis。 当下载它之后,提取文件到phpredis目录。在Ubuntu上,安装以下扩展。

cd phpredis 
sudo phpize 
sudo ./configure 
sudo make 
sudo make install
Shell

现在,将“modules”文件夹的内容复制并粘贴到PHP扩展目录中,并在php.ini中添加以下行。

extension = redis.so
Shell

现在,Redis PHP安装完成!

使用连接到Redis服务器

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //check whether server is running or not 
   echo "Server is running: ".$redis->ping(); 
?>
PHP

当程序执行时,将产生以下结果。

Connection to server sucessfully 
Server is running: PONG
Shell

Redis PHP字符串示例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //set the data in redis string 
   $redis->set("tutorial-name", "Redis tutorial"); 
   // Get the stored data and print it 
   echo "Stored string in redis:: " .$redisget("tutorial-name"); 
?>
PHP

执行上面代码,将生成以下结果 -

Connection to server sucessfully 
Stored string in redis:: Redis tutorial
Java

Redis php列表示例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //store data in redis list 
   $redis->lpush("tutorial-list", "Redis"); 
   $redis->lpush("tutorial-list", "Mongodb"); 
   $redis->lpush("tutorial-list", "Mysql");  

   // Get the stored data and print it 
   $arList = $redis->lrange("tutorial-list", 0 ,5); 
   echo "Stored string in redis:: "; 
   print_r($arList); 
?>
PHP

执行上面代码,将生成以下结果 -

Connection to server sucessfully 
Stored string in redis:: 
Redis 
Mongodb 
Mysql
PHP

Redis php键示例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   // Get the stored keys and print it 
   $arList = $redis->keys("*"); 
   echo "Stored keys in redis:: " 
   print_r($arList); 
?>
PHP

执行上面代码,将生成以下结果 -

Connection to server sucessfully 
Stored string in redis:: 
tutorial-name 
tutorial-list
PHP

易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值