/**
* 产生弹幕数据
* php index.php cli goods gen_barrage_data
*/
public function gen_barrage_data()
{
$cache_key = CACHE_KEY_PREFIX . 'barrage';
$cache_data = [];
$s_time = time();
$this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'file'));
$this->load->model('member/member_go_record');
$sleep_time = 1;
while (true) {
//超过一分钟退出程序
if (time() - $s_time >= 60) {
exit("到了一分钟的时间,结束此次进程");
}
//读取最新的购买记录的id
$more_than_id = 0;
if ($this->cache->get(CACHE_KEY_PREFIX . 'barrage_max_id')) {
$more_than_id = $this->cache->get(CACHE_KEY_PREFIX . 'barrage_max_id');
}
//查询新的购买记录数据
$barrage_data = $this->member_go_record->get_barrage_data($more_than_id);
if (empty($barrage_data)) {
echo "no new data
";
sleep($sleep_time);
continue;
}
//记录弹幕数据到缓存
foreach ($barrage_data as &$v) {
$tmp_ip = explode(',', $v['ip']);
$v['ip'] = $this->deal_ip_address($tmp_ip[0]);
}
$old_barrage_data = $this->cache->get($cache_key);
$old_barrage_data = json_decode($old_barrage_data, TRUE);
if (!is_array($old_barrage_data)) {
$old_barrage_data = [];
}
$barrage_data = array_merge($barrage_data, $old_barrage_data);
$barrage_data = array_slice($barrage_data, 0, 10);
$this->cache->save($cache_key, json_encode($barrage_data), TIME_HOUR);
//记录最新的id到缓存
$max_id = $barrage_data[0]['id'];
var_dump($max_id);
$this->cache->save(CACHE_KEY_PREFIX . 'barrage_max_id', $max_id, TIME_HOUR);
sleep($sleep_time);
}
}