1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- use think\Db;
- use Redis;
- class Plantask extends Controller
- {
- public function index()
- {
- exit;
- }
- //昨天的pvuv数据从redis转移到database 并清空昨天的redis
- public function auto_pvuv_redis_to_database(){
- //开启redis
- $redis = new Redis();
- $config = config('redis');
- $redis->connect($config['redis_host'], $config['redis_port']);
- if ($config['redis_pwd']) {
- $redis->auth($config['redis_pwd']);
- }
- if($config['redis_selectdb'] > 0){
- $redis->select($config['redis_selectdb']);
- }
- //日期
- $today = date('Y-m-d');
- $yestodaytime = strtotime($today)-86400;
- $yestoday = date('Y-m-d',$yestodaytime);
- //$yestoday = $today;//测试临时用
- //视频id 列表
- $yestoday_video_key = $yestoday.'_video_list';
- $yestoday_video_list = $redis->sMembers($yestoday_video_key);
- if(empty($yestoday_video_list)){
- echo $yestoday_video_key.': empty';exit;
- }
- foreach($yestoday_video_list as $key => $video_id){
- //准备数据
- $date_pvuv = [
- 'video_id' => $video_id,
- 'datetime' => $yestodaytime,
- ];
- //昨日视频pv
- $yestoday_pv_key = $yestoday.'_video_pv_'.$video_id;
- $date_pvuv['video_pv'] = $redis->get($yestoday_pv_key);
- $redis->del($yestoday_pv_key);
- //昨日视频uv
- $yestoday_uv_key = $yestoday.'_video_uv_'.$video_id;
- $date_pvuv['video_uv'] = $redis->sCard($yestoday_uv_key); //数量
- $redis->del($yestoday_uv_key);
- //写入数据
- Db::name('video_pvuv')->where($date_pvuv)->delete();
- Db::name('video_pvuv')->insertGetId($date_pvuv);
- }
- $redis->del($yestoday_video_key);
- }
- }
|