• 技术文章 >后端开发 >php教程

    PHP链接redis的方法代码

    小云云小云云2018-03-07 09:08:09原创2275
    在php程序中使用Redis之前,需要确保在机器上安装了Redis的PHP驱动程序和PHP环境。可以先在将PHP电脑上并配置好环境。本文就和大家分享PHP链接redis的方法代码,希望能帮助到大家。

    安装

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

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

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

    extension = redis.so

    现在,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

    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:: " .$redis→get("tutorial-name"); ?>

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

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

    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); ?>

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

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

    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); ?>

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

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

    相关推荐:

    PHP操作Redis实例讲解

    一个php+redis的简单实例分享

    Redis在PHP的一些使用方法

    以上就是PHP链接redis的方法代码的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:redis php 代码
    上一篇:DB2死锁的解决过程全记录 下一篇:PHP周时间代码详解
    PHP编程就业班

    相关文章推荐

    • PHP高并发实例详解之解决商品库存超卖问题• 四十分求ThinkPHP循环嵌套打印出父类与子类的列表• 请问一个正则有关问题这么写问什么匹配不对 • 这个登录处理代码错了,不知道如何解决 • 关于退选学程的代码,系统提示未定义变量

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网