StatisticCache.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Cache;
  3. use App\Consts\ErrorConst;
  4. use App\Libs\Utils;
  5. use Illuminate\Support\Facades\Redis;
  6. class StatisticCache
  7. {
  8. /**
  9. * 以下传参的通用说明
  10. * key_name的具体传参值从CacheKeys.php中获取,以statistic为前缀
  11. * day的值默认为当天,可通过传不同日期增删改对应的值,day的格式为date('Ymd')
  12. */
  13. /**
  14. * 获取pv
  15. * @param $key_name
  16. * @param $day
  17. * @return mixed
  18. */
  19. public static function getPV($key_name, $day = ''): int
  20. {
  21. $key_name = 'statistic.' . $key_name . '_pv';
  22. if (!$day) $day = date('Ymd');
  23. $cacheKey = Utils::getCacheKey($key_name, [$day]);
  24. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  25. $result = Redis::get($cacheKey);
  26. return $result ? $result : 0;
  27. }
  28. /**
  29. * 设置pv
  30. *
  31. * @param $key_name
  32. * @param $day
  33. * @return mixed
  34. */
  35. public static function setPV($key_name, $day = '')
  36. {
  37. $key_name = 'statistic.' . $key_name . '_pv';
  38. if (!$day) $day = date('Ymd');
  39. $cacheKey = Utils::getCacheKey($key_name, [$day]);
  40. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  41. return Redis::incr($cacheKey);
  42. }
  43. /**
  44. * 删除pv
  45. *
  46. * @param $key_name
  47. * @param $day
  48. * @return mixed
  49. */
  50. public static function delPV($key_name, $day = '')
  51. {
  52. $key_name = 'statistic.' . $key_name . '_pv';
  53. if (!$day) $day = date('Ymd');
  54. $cacheKey = Utils::getCacheKey($key_name, [$day]);
  55. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  56. return Redis::del($cacheKey);
  57. }
  58. /**
  59. * 获取uv
  60. *
  61. * @param $key_name
  62. * @param $day
  63. * @return mixed
  64. */
  65. public static function getUV($key_name, $day = ''): int
  66. {
  67. $key_name = 'statistic.' . $key_name . '_uv';
  68. if (!$day) $day = date('Ymd');
  69. $cacheKey = Utils::getCacheKey($key_name, [$day]);
  70. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  71. // $result = Redis::pfcount($cacheKey);
  72. $result = Redis::scard($cacheKey);
  73. return $result ? $result : 0;
  74. }
  75. /**
  76. * 设置uv
  77. *
  78. * @param $key_name
  79. * @param $day
  80. * @param $uid
  81. * @return mixed
  82. */
  83. public static function setUV($key_name, $uid, $day = '')
  84. {
  85. $key_name = 'statistic.' . $key_name . '_uv';
  86. if (!$day) $day = date('Ymd');
  87. $cacheKey = Utils::getCacheKey($key_name, [$day]);
  88. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  89. // return Redis::pfadd($cacheKey, $uid);
  90. return Redis::sadd($cacheKey, $uid);
  91. }
  92. /**
  93. * 删除uv
  94. *
  95. * @param $key_name
  96. * @param $day
  97. * @return mixed
  98. */
  99. public static function delUV($key_name, $day = '')
  100. {
  101. $key_name = 'statistic.' . $key_name . '_uv';
  102. if (!$day) $day = date('Ymd');
  103. $cacheKey = Utils::getCacheKey($key_name, [$day]);
  104. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  105. return Redis::del($cacheKey);
  106. }
  107. /**
  108. * 获取模板uv
  109. *
  110. * @param $template_id
  111. * @return mixed
  112. */
  113. public static function getTemplateUV($template_id): int
  114. {
  115. $key_name = 'statistic.template_uv';
  116. $cacheKey = Utils::getCacheKey($key_name, [$template_id]);
  117. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  118. // $result = Redis::pfcount($cacheKey);
  119. $result = Redis::scard($cacheKey);
  120. return $result ? $result : 0;
  121. }
  122. /**
  123. * 设置模板uv
  124. *
  125. * @param $uid
  126. * @param $template_id
  127. * @return mixed
  128. */
  129. public static function setTemplateUV($uid, $template_id = 0)
  130. {
  131. $key_name = 'statistic.template_uv';
  132. $cacheKey = Utils::getCacheKey($key_name, [$template_id]);
  133. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  134. // return Redis::pfadd($cacheKey, $uid);
  135. return Redis::sadd($cacheKey, $uid);
  136. }
  137. /**
  138. * 删除模板uv
  139. *
  140. * @param $template_id
  141. * @return mixed
  142. */
  143. public static function delTemplateUV($template_id = 0)
  144. {
  145. $key_name = 'statistic.template_uv';
  146. $cacheKey = Utils::getCacheKey($key_name, [$template_id]);
  147. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  148. return Redis::del($cacheKey);
  149. }
  150. /**
  151. * 获取订阅统计总记录数目(按日)
  152. *
  153. * @param $day
  154. * @return mixed
  155. */
  156. public static function getSubscribeCount($day = '')
  157. {
  158. if (!$day) $day = date('Ymd');
  159. $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
  160. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  161. $result = Redis::llen($cacheKey);
  162. return $result ? $result : 0;
  163. }
  164. /**
  165. * 获取订阅统计(按日)
  166. *
  167. * @param $day
  168. * @param $start_index
  169. * @param $end_index
  170. * @return mixed
  171. */
  172. public static function getSubscribe($day = '', $start_index = 0, $end_index = -1)
  173. {
  174. if (!$day) $day = date('Ymd');
  175. $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
  176. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  177. $result = Redis::lrange($cacheKey, $start_index, $end_index);
  178. return $result ? $result : [];
  179. }
  180. /**
  181. * 设置订阅统计(按日)
  182. *
  183. * @param $data
  184. * @param $day
  185. * @return mixed
  186. */
  187. public static function setSubscribe(array $data, $day = '')
  188. {
  189. if (!$day) $day = date('Ymd');
  190. $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
  191. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  192. if (!Redis::exists($cacheKey)) {
  193. $result = Redis::lpush($cacheKey, json_encode($data, true));
  194. $seven_days_later = date('Y-m-d 23:59:59', strtotime('+7 day'));
  195. $expire_at = strtotime($seven_days_later) - time();
  196. Redis::expire($cacheKey, $expire_at);
  197. } else {
  198. $result = Redis::lpush($cacheKey, json_encode($data, true));
  199. }
  200. return $result;
  201. }
  202. /**
  203. * 删除订阅统计(按日)
  204. *
  205. * @param $day
  206. * @return mixed
  207. */
  208. public static function delSubscribe($day = '')
  209. {
  210. if (!$day) $day = date('Ymd');
  211. $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
  212. if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
  213. return Redis::del($cacheKey);
  214. }
  215. }