Plantask.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use Redis;
  6. class Plantask extends Controller
  7. {
  8. public function index()
  9. {
  10. exit;
  11. }
  12. //昨天的pvuv数据从redis转移到database 并清空昨天的redis
  13. public function auto_pvuv_redis_to_database(){
  14. //开启redis
  15. $redis = new Redis();
  16. $config = config('redis');
  17. $redis->connect($config['redis_host'], $config['redis_port']);
  18. if ($config['redis_pwd']) {
  19. $redis->auth($config['redis_pwd']);
  20. }
  21. if($config['redis_selectdb'] > 0){
  22. $redis->select($config['redis_selectdb']);
  23. }
  24. //日期
  25. $today = date('Y-m-d');
  26. $yestodaytime = strtotime($today)-86400;
  27. $yestoday = date('Y-m-d',$yestodaytime);
  28. //$yestoday = $today;//测试临时用
  29. //视频id 列表
  30. $yestoday_video_key = $yestoday.'_video_list';
  31. $yestoday_video_list = $redis->sMembers($yestoday_video_key);
  32. if(empty($yestoday_video_list)){
  33. echo $yestoday_video_key.': empty';exit;
  34. }
  35. foreach($yestoday_video_list as $key => $video_id){
  36. //准备数据
  37. $date_pvuv = [
  38. 'video_id' => $video_id,
  39. 'datetime' => $yestodaytime,
  40. ];
  41. //昨日视频pv
  42. $yestoday_pv_key = $yestoday.'_video_pv_'.$video_id;
  43. $date_pvuv['video_pv'] = $redis->get($yestoday_pv_key);
  44. $redis->del($yestoday_pv_key);
  45. //昨日视频uv
  46. $yestoday_uv_key = $yestoday.'_video_uv_'.$video_id;
  47. $date_pvuv['video_uv'] = $redis->sCard($yestoday_uv_key); //数量
  48. $redis->del($yestoday_uv_key);
  49. //写入数据
  50. Db::name('video_pvuv')->where($date_pvuv)->delete();
  51. Db::name('video_pvuv')->insertGetId($date_pvuv);
  52. }
  53. $redis->del($yestoday_video_key);
  54. }
  55. }