Files
amb_wechatapp/application/api/model/data/Traffic.php
T

56 lines
1.4 KiB
PHP

<?php
namespace app\api\model\data;
use think\Model;
use think\Cache;
class Traffic extends Model
{
// 表名
protected $name = 'data_traffic';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
];
public function cacheToDatabses(){
$cacheKey = \app\common\constant\cache\Keys::RECORD_LOG;
$batchSize = 100; // 每次处理的批量大小
// 从缓存中获取所有日志数据
$queue = Cache::get($cacheKey) ?: [];
if (empty($queue)) {
return;
}
try {
// 准备批量插入的数据
$insertData = [];
// 从队列中取出最多 $batchSize 条记录
while (!empty($queue) && count($insertData) < $batchSize) {
$insertData[] = array_shift($queue);
}
// 批量插入数据库
if (!empty($insertData)) {
self::insertAll($insertData);
}
// 将处理后剩余的队列写回缓存
Cache::set($cacheKey, $queue);
} catch (\Exception $e) {
// 写回缓存
Cache::set($cacheKey, []);
}
}
}