ITPub博客

首页 > IT职业 > IT职场 > redis连接php

redis连接php

IT职场 作者:cityvag 时间:2021-09-09 09:36:21 0 删除 编辑


安装在PHP程序中使用Redis,需要确保我们有Redis的PHP驱动程序和PHP安装设置在机器上。可以查看PHP教程教你如何在机器上安装PHP。现在,让我们来看看一下如何设置Redis的PHP驱动程序。需要从github上资料库https://github.com/nicolasff/phpredis下载phpredis。下载了它以后,将文件解压缩到phpredis目录。在Ubuntu上安装这个扩展,如下图所示。cd phpredissudo phpizesudo ./configuresudo makesudo make install现在,复制和粘贴“modules”文件夹的内容复制到PHP扩展目录中,并在php.ini中添加以下几行。extension = redis.so现在Redis和PHP安装完成。连接到Redis服务器   //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();?>当执行程序时,会产生下面的结果:Connection to server sucessfullyServer is running: PONGRedis的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:: " + jedis.get("tutorial-name");?>当执行程序时,会产生下面的结果:Connection to server sucessfullyStored string in redis:: Redis tutorialRedis的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);?>当执行程序时,会产生下面的结果:Connection to server sucessfullyStored string in redis::RedisMongodbMysqlRedis的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);?>当执行程序时,会产生下面的结果:Connection to server sucessfullyStored string in redis::tutorial-nametutorial-list


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3518/viewspace-2805098/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    79
  • 访问量
    742