IdentifyDayMotLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * IdentifyDayMotLogic.php UTF-8
  4. * 玩家每日在线时长统计逻辑处理
  5. *
  6. * @date : 2019/11/29 21:40
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.5
  11. */
  12. namespace huoIdentify\logic;
  13. use huo\model\common\CommonModel;
  14. use huoIdentify\model\IdentifyDayMotModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use think\Cache;
  18. class IdentifyDayMotLogic extends CommonModel {
  19. protected $update_time = CommonConst::MINUTE_SECONDS_10; //写入数据库间隔时间 10分钟 60s
  20. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_DAY_MOT_PREFIX; //缓存前缀
  21. /**
  22. * 获取缓存key
  23. *
  24. * @param string $date 日期
  25. * @param int $mem_id 玩家ID
  26. *
  27. * @return string
  28. */
  29. protected function getCacheKeyByDateMem($date, $mem_id) {
  30. $date = date('Ymd', strtotime($date));
  31. return $this->cache_key_prefix.$date.$mem_id;
  32. }
  33. /**
  34. * 保存缓存数据
  35. *
  36. * @param $key
  37. * @param $data
  38. */
  39. public function saveCacheData($key, $data) {
  40. $_expire = CommonConst::CONST_7_DAY_SECONDS; //缓存7天
  41. Cache::set($key, $data, $_expire);
  42. }
  43. /**
  44. * 获取信息
  45. *
  46. * @param string $date 日期
  47. * @param int $mem_id 玩家ID
  48. *
  49. * @return array|false
  50. */
  51. public function getInfoByDateMem($date, $mem_id) {
  52. $date = date('Y-m-d', strtotime($date));
  53. /* 缓存操作 */
  54. $_cache_key = $this->getCacheKeyByDateMem($date, $mem_id);
  55. $_data = Cache::get($_cache_key);
  56. if (!empty($_data)) {
  57. return $_data;
  58. }
  59. $_idm_model = new IdentifyDayMotModel();
  60. $_data = $_idm_model->getInfoByDateMem($date, $mem_id);
  61. if (empty($_data)) {
  62. $_data = [
  63. 'date' => $date,
  64. 'mem_id' => $mem_id
  65. ];
  66. $_idm_model->addData($_data);
  67. $_data = $_idm_model->getInfoByDateMem($date, $mem_id);
  68. }
  69. $_next_update_time = time() + $this->update_time;
  70. $_data['next_update_time'] = $_next_update_time;
  71. $this->saveCacheData($_cache_key, $_data);
  72. return $_data;
  73. }
  74. /**
  75. * 更新信息
  76. *
  77. * @param array $data 更新数据
  78. * @param string $date 日期
  79. * @param int $mem_id 玩家ID
  80. *
  81. * @return bool
  82. */
  83. public function updateDataByDateMem($data, $date, $mem_id) {
  84. $_cache_key = $this->getCacheKeyByDateMem($date, $mem_id);
  85. $_old_data = $this->getInfoByDateMem($date, $mem_id);
  86. $_data = array_merge($_old_data, $data);
  87. $_time = time();
  88. $_update_time = $_data['next_update_time']; //获取当前缓存的更新时间 入库失败则回退
  89. $_data['update_time'] = $_time;
  90. $this->saveCacheData($_cache_key, $_data); //先更新缓存
  91. if ($_update_time <= $_time) {
  92. $_data['next_update_time'] = $_time + $this->update_time;
  93. $_rs = (new IdentifyDayMotModel())->updateData($_data, $_data['id']);
  94. if (false === $_rs) {
  95. /* 入库失败 */
  96. $_data['next_update_time'] = $_update_time; //还原更新时间
  97. }
  98. $this->saveCacheData($_cache_key, $_data); //更新缓存
  99. }
  100. return true;
  101. }
  102. /**
  103. * 更新时长
  104. *
  105. * @param int $time 时长
  106. * @param int $mem_id 玩家id
  107. *
  108. * @return bool
  109. */
  110. public function updateOnlineTimeByMemId($time, $mem_id) {
  111. $_date = date('Y-m-d');
  112. $_data = $this->getInfoByDateMem($_date, $mem_id);
  113. $_data['online_duration'] += $time;
  114. return $this->updateDataByDateMem($_data, $_date, $mem_id);
  115. }
  116. }