最近做了一个聊天室系统,系统有N个聊天室,在并发量很大的情况下,如果用户聊天记录直接写入MySql的话,对数据库的冲击会很大。这里我写了个简单的解决方案:聊天数据先存入redis的队列中,然后用crontab每隔1分钟执行PHP脚本,把数据从redis队列中“批量”移到mysql。当有多条插入语句时,把数据拼装成1条insert语句,减少数据库的操作次数。
1. 配置文件 config.php
<?php
function dbMessageConfig(){
//用户信息库表的配置
return array(
‘database_type‘ => ‘mysql‘,
‘database_name‘ => ‘test‘,
‘server‘ => ‘127.0.0.1‘,
‘username‘ => ‘root‘,
‘password‘ => ‘root‘,
‘charset‘ => ‘utf8‘
);
}
function redisCacheConfig(){
return array(
‘ip‘ => ‘127.0.0.1‘,
‘port‘ => ‘6379‘,
‘password‘ => ‘888888‘
);
}
2. redis2mysql.php
<?php
error_reporting(E_ALL^E_NOTICE^E_WARNING);
require_once dirname(__FILE__).‘/config.php‘;
$dbMessage = dbMessageConfig();
$configRedis = redisCacheConfig();
$r = new \Redis();
$r->connect($configRedis[‘ip‘], $configRedis[‘port‘]);
$r->auth($configRedis[‘password‘]);
//获取redis中的消息
$message = $r->rpop(‘chat_message‘);
$tenantArr = array();
if(!empty($message)){
$tmpArr = explode(‘:‘, $message);
$tenant_id = $tmpArr[0];
if(!empty($tenant_id) && is_numeric($tenant_id)){
if(!empty($tmpArr[1])) $tenantArr[$tenant_id] = $tmpArr[1];
}
}
//一次最多取20条记录
$MAX_NUM = 20;
$index = 1;
while(!empty($message) && $MAX_NUM > $index){
$message = $r->rpop(‘chat_message‘);
//echo $message;
$tmpArr = explode(‘:‘, $message);
$tenant_id = $tmpArr[0];
if(!empty($tenant_id) && is_numeric($tenant_id)){
if(!empty($tmpArr[1])){
if(array_key_exists($tenant_id, $tenantArr)){
$tmp = $tenantArr[$tenant_id];
$tmp = $tmp.",".$tmpArr[1];
$tenantArr[$tenant_id] = $tmp;
}else{
$tenantArr[$tenant_id] = $tmpArr[1];
}
}
}
$index = $index + 1;
}
$table_message = ‘chat_message‘;
$dbh = new \PDO(‘mysql:host=‘.$dbMessage[‘server‘].‘;dbname=‘.$dbMessage[‘database_name‘], $dbMessage[‘username‘], $dbMessage[‘password‘]);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec(‘set names ‘.$dbMessage[‘charset‘]);
foreach ($tenantArr as $tenant_id=>$sql) {
$sqlstr = ‘insert INTO ‘.$table_message.‘_‘.$tenant_id.‘ VALUES ‘.$sql.‘;‘;
$stmt = $dbh->prepare($sqlstr);
$stmt->execute();
}
3. 配置crontab
*/1 * * * * /opt/php/bin/php -f /usr/local/php/redis2mysql.php >/dev/null 2>&1
启动crontab:/sbin/service crond start
4. 写入redis的sql语句(java代码):
//把chatmessage发送到redis储存
Jedis redis = RedisUtil.getJedis();
String key="message";
StringBuffer strMsg = new StringBuffer(tenant_id);
strMsg.append(":(NULL,");
strMsg.append(room_id);
strMsg.append(",");
strMsg.append(uid);
strMsg.append(",‘");
strMsg.append(username);
strMsg.append("‘,");
strMsg.append(user_type);
strMsg.append(",‘");
strMsg.append(avatar);
strMsg.append("‘,‘");
strMsg.append(content);
strMsg.append("‘,");
strMsg.append(add_time);
strMsg.append(",1)");
redis.lpush(key, strMsg.toString());
参考:
⑴ crontab启动 、运行和编辑查看
http://blog.csdn.net/ariessurfer/article/details/7459183
⑵ crontab定时任务配置
http://www.cnblogs.com/kerrycode/p/3238346.html
⑶ php mysql PDO使用
http://blog.csdn.net/qq635785620/article/details/11284591