连接Redis服务

阅读数:1219 发布时间:2016-09-30 08:26:21

作者:w4why 标签: redis PHP 连接

连接到 redis 服务

    <?php
        //连接本地的 Redis 服务
       $redis = new Redis();
       $redis->connect('127.0.0.1', 6379);
       echo "Connection to server sucessfully";
             //查看服务是否运行
       echo "Server is running: " . $redis->ping();
    ?>
Tips:如果需要修改链接ip,需要修改 /etc/redis/redis.conf下的默认ip设置.

若文件输出结果为:
Connection to server sucessfully Server is running: PONG 则表示链接成功

Redis下 String实例

    <?php
       //连接本地的 Redis 服务
       $redis = new Redis();
       $redis->connect('127.0.0.1', 6379);
       echo "Connection to server sucessfully";
       //设置 redis 字符串数据
       $redis->set("tutorial-name", "Redis tutorial");
       // 获取存储的数据并输出
       echo "Stored string in redis:: " . $redis->get("tutorial-name");
    ?>  

相关文章推荐: