redis连接php
安装在PHP程序中使用Redis,需要确保我们有Redis的PHP驱动程序和PHP安装设置在机器上。可以查看PHP教程教你如何在机器上安装PHP。现在,让我们来看看一下如何设置Redis的PHP驱动程序。需要从github上资料库https://github.com/nicolasff/phpredis下载phpredis。下载了它以后,将文件解压缩到phpredis目录。在Ubuntu上安装这个扩展,如下图所示。cdphpredissudophpizesudo./configuresudomakesudomakeinstall现在,复制和粘贴“modules”文件夹的内容复制到PHP扩展目录中,并在php.ini中添加以下几行。extension=redis.so现在Redis和PHP安装完成。连接到Redis服务器<?php//ConnectingtoRedisserveronlocalhost$redis=newRedis();$redis->connect('127.0.0.1',6379);echo"Connectiontoserversucessfully";//checkwhetherserverisrunningornotecho"Serverisrunning:"+$redis->ping();?>当执行程序时,会产生下面的结果:ConnectiontoserversucessfullyServerisrunning:PONGRedis的PHP字符串实例<?php//ConnectingtoRedisserveronlocalhost$redis=newRedis();$redis->connect('127.0.0.1',6379);echo"Connectiontoserversucessfully";//setthedatainredisstring$redis->set("tutorial-name","Redistutorial");//Getthestoreddataandprintitecho"Storedstringinredis::"+jedis.get("tutorial-name");?>当执行程序时,会产生下面的结果:ConnectiontoserversucessfullyStoredstringinredis::RedistutorialRedis的PHP列表示例<?php//ConnectingtoRedisserveronlocalhost$redis=newRedis();$redis->connect('127.0.0.1',6379);echo"Connectiontoserversucessfully";//storedatainredislist$redis->lpush("tutorial-list","Redis");$redis->lpush("tutorial-list","Mongodb");$redis->lpush("tutorial-list","Mysql");//Getthestoreddataandprintit$arList=$redis->lrange("tutorial-list",0,5);echo"Storedstringinredis::"print_r($arList);?>当执行程序时,会产生下面的结果:ConnectiontoserversucessfullyStoredstringinredis::RedisMongodbMysqlRedis的PHP键例<?php//ConnectingtoRedisserveronlocalhost$redis=newRedis();$redis->connect('127.0.0.1',6379);echo"Connectiontoserversucessfully";//Getthestoredkeysandprintit$arList=$redis->keys("*");echo"Storedkeysinredis::"print_r($arList);?>当执行程序时,会产生下面的结果:ConnectiontoserversucessfullyStoredstringinredis::tutorial-nametutorial-list
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。