Phpredisadmin:github address graphical management interface
git clone https://github.com/ErikDubbelboer/phpRedisAdmin.gitcd phpredisadmingit clone https://github.com/nrk/ Predis.git Vendor
First, install the Redis graphical management interface above to easily manage Redis data
In order to reduce the concurrency of MySQL, the user's microblog is first present in Redis
Suppose users need three fields when publishing, UID (user ID number), username (user name), content (' User's comments ')
For example, the user passes the following information
?
123456789101112131415161718 |
<?php
//此处需要安装phpredis
$redis
=
new Redis();
$redis
->connect(
‘127.0.0.1‘
, 6379);
// 连接redis
$web_info
=
array
(
‘uid‘
=>
‘123456‘
,
‘username‘ =>
‘123‘
,
‘content‘
=>
‘123‘
);
//将数组转成json来存储
$list
= json_encode(
$web_info
);
//lpush向KEY对应的头部添加一个字符串元素
$redis
->lpush(
‘weibo_lists‘
,
$list
);
$redis
->close();
///var_dump(json_encode($web_info));
var_dump(
$list
);
?>
|
Here you can see that our Redis already has data.
?
123456789101112131415161718192021222324 |
//创建一个PDO数据库链接 data.php
<?php
class
qq{
public
function
post(
$uid
=
‘‘
,
$username
=
‘‘
,
$content
=
‘‘
){
try
{
$dsn
=
"mysql:host;dbname=localhost;dbname=test"
;
$db
=
new
PDO(
$dsn
,
‘root‘
,
‘root‘
);
$db
->
exec
(
"SET NAMES UTF8"
);
$sql
=
"insert into test(uid,username,content)values(‘$uid‘,‘$username‘,‘$content‘)"
;
$db
->
exec
(
$sql
);
}
catch
(PDOException
$e
){
$e
->getMessage();
}
}
}
|
?
12345678910111213141516171819202122 |
//处理redis数据库的数据 并把数据放到MYSQL数据库中
<?php
include
"data.php"
;
$qq
=
new
qq();
$redis =
new
Redis();
$redis
->connect(
‘127.0.0.1‘
, 6379);
//返回的列表的大小。如果列表不存在或为空,该命令返回0。如果该键不是列表,该命令返回FALSE
if
(
$redis
-> lsize(
‘weibo_lists‘
)){
//从LIST头部删除并返回删除数据
$info
=
$redis
->rpop(
‘weibo_lists‘
);
$info
= json_decode(
$info
);
$qq
->post(
$info
->uid,
$info
->username,
$info
->content);
}
$redis
->close();
var_dump(
$info
); ?>
|
We can see that the database already has data.
Publish Weibo under PHP using Redis Message Queuing (copy)