Redis学习7之Predis连接redis过程和测试

来源:互联网 发布:2017淘宝新店怎么做 编辑:程序博客网 时间:2024/06/16 12:54

Predis连接redis过程和测试:
1.下载地址:https://github.com/nrk/predis
可以git clone或者下载压缩包再解压到redis的clients文件夹下;

2.安装apache和php

sudo apt-get install  apache2 php5

3.在/var/www/html下新建test1.php,代码内容为:

<?phpecho "hello world</br>";require '/home/xubo/cloud/redis/clients/predis/autoload.php';//require './predis/autoload.php';echo "hello xubo1</br>";/* for ($i = 0; $i < 20; $i++) {        echo $i;        echo "</br>";    }*//*$redis = new Predis\Client([      'scheme'=>'tcp',      'host' => '127.0.0.1',      'port' => 7000,]);*//*$redis = new Predis\Client();try {echo $redis->get('xubo');}catch(Exception $e){{echo "Message:{$e->getMessage()}";}*/$redis = new Predis\Client();echo $redis-> get('foo');echo "</br>hello xubo2</br>";//echo $redis->get('foo');//$client = new Predis\Client();//$client->set('foo', 'bar');//$value = $client->get('foo');//echo $redis;for ($i=0;$i<10;$i++){$client = new Predis\Client();$client -> set("xubo$i",$i);echo $client->get("xubo$i");        echo '</br>'; }echo "hello xubo end";?>

输出结果为: 查看为127.0.0.1/test1.php

hello worldhello xubo1barhello xubo20123456789hello xubo end

在redis-cli下查看为:

xubo@xubo:~/cloud/redis/clients/predis/tests$ redis-cli 127.0.0.1:6379> keys * 1) "xubo8" 2) "xubo2" 3) "xubo6" 4) "xubo0" 5) "xubo5" 6) "key:__rand_int__" 7) "xubo1" 8) "xubo3" 9) "xubo"10) "xubo4"11) "xubo9"12) "xubo7"13) "mylist"14) "foo"15) "counter:__rand_int__"

说明predis连接redis且set和get操作成功。

0 0
原创粉丝点击