您的位置:首页 > 数据库 > Redis

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

2015-09-16 14:35 691 查看
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,代码内容为:

<?php
echo "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 world
hello xubo1
bar
hello xubo2
0
1
2
3
4
5
6
7
8
9
hello 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操作成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息