UserCache.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace App\Cache;
  3. use App\Consts\BaseConst;
  4. use App\Libs\Utils;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Redis;
  7. class UserCache
  8. {
  9. /**
  10. * 获取token数据
  11. * @param $token
  12. * @return array|mixed
  13. */
  14. public static function getTokenData($token)
  15. {
  16. if (empty($token)) {
  17. return [];
  18. }
  19. $cacheKey = Utils::getCacheKey('user.token', [$token]);
  20. $result = Redis::get($cacheKey);
  21. return $result ? json_decode($result, true) : [];
  22. }
  23. /**
  24. * 设置token数据
  25. *
  26. * @param $token
  27. * @param $data
  28. * @return mixed
  29. */
  30. public static function setTokenData($token, $data)
  31. {
  32. if (empty($token)) {
  33. return false;
  34. }
  35. $cacheKey = Utils::getCacheKey('user.token', [$token]);
  36. return Redis::set($cacheKey, json_encode($data, JSON_UNESCAPED_UNICODE));
  37. }
  38. /**
  39. * 删除token数据
  40. *
  41. * @param $token
  42. * @return mixed
  43. */
  44. public static function delTokenData($token)
  45. {
  46. if (empty($token)) {
  47. return false;
  48. }
  49. $cacheKey = Utils::getCacheKey('user.token', [$token]);
  50. return Redis::del($cacheKey);
  51. }
  52. /**
  53. * 设置最近阅读数据
  54. * @param $uid
  55. * @param $data
  56. * @return false
  57. */
  58. public static function setRecentBooks($uid, $data) {
  59. if (empty($uid)) {
  60. return false;
  61. }
  62. $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]);
  63. if (!Redis::exists($cacheKey)) {
  64. Redis::hset($cacheKey, $data['bid'], json_encode($data, JSON_UNESCAPED_UNICODE));
  65. Redis::expire($cacheKey, BaseConst::ONE_WEEK_SECONDS);
  66. }else {
  67. Redis::hset($cacheKey, $data['bid'], json_encode($data, JSON_UNESCAPED_UNICODE));
  68. }
  69. return true;
  70. }
  71. /**
  72. * 获取最近阅读数据(bid)
  73. * @param $uid
  74. * @param $bid
  75. * @return array|false|mixed
  76. */
  77. public static function getRecentBooksByBid($uid, $bid) {
  78. if (empty($uid)) {
  79. return false;
  80. }
  81. $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]);
  82. if (Redis::hExists($cacheKey, $bid)) {
  83. $json = Redis::hget($cacheKey, $bid);
  84. return json_decode($json, true);
  85. }else {
  86. return [];
  87. }
  88. }
  89. /**
  90. * 获取最近阅读数据(keys)
  91. * @param $uid
  92. * @return array|mixed
  93. */
  94. public static function getRecentBooksKeys($uid) {
  95. if (empty($uid)) {
  96. return false;
  97. }
  98. $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]);
  99. $result = Redis::hkeys($cacheKey);
  100. return $result ? $result : [];
  101. }
  102. /**
  103. * 获取最近阅读数据(values)
  104. * @param $uid
  105. * @return array|mixed
  106. */
  107. public static function getRecentBooksValues($uid) {
  108. if (empty($uid)) {
  109. return false;
  110. }
  111. $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]);
  112. if (Redis::exists($cacheKey)) {
  113. $result = Redis::hvals($cacheKey);
  114. $data = [];
  115. if ($result) {
  116. foreach ($result as $item) {
  117. $data[] = json_decode($item, true);
  118. }
  119. }
  120. }else { // key不存在时取数据库
  121. $table = 'record_records'.($uid % 2048);
  122. $result = DB::connection('read_record_mysql')->table($table)->where('uid', $uid)->groupBy('bid')
  123. ->select('bid',
  124. DB::raw("(select max(created_at) from {$table} as a where a.bid = {$table}.bid) as read_time"),
  125. DB::raw("(select max(sequence) from {$table} as a where a.bid = {$table}.bid) as sequence"))->get();
  126. $data = [];
  127. foreach ($result as $item) {
  128. $book_chapter = DB::table('chapters as c')->leftJoin('books as b', 'b.id', 'c.bid')
  129. ->where(['c.bid'=>getProp($item, 'bid'), 'c.sequence'=>getProp($item, 'sequence'), 'c.is_check'=>1, 'c.is_deleted'=>0])
  130. ->select('c.bid', 'b.name as book_name', 'c.id as cid', 'c.name as chapter_name', 'c.sequence')->first();
  131. if ($book_chapter) {
  132. $book_chapter = (array)$book_chapter;
  133. $book_chapter['read_time'] = getProp($item, 'read_time');
  134. }
  135. Redis::hset($cacheKey, $book_chapter['bid'], json_encode($book_chapter, JSON_UNESCAPED_UNICODE));
  136. $data[] = $book_chapter;
  137. }
  138. Redis::expire($cacheKey, BaseConst::ONE_WEEK_SECONDS);
  139. }
  140. return $data;
  141. }
  142. /**
  143. * 获取最近阅读数据(all)
  144. * @param $uid
  145. * @return mixed|array
  146. */
  147. public static function getRecentBooksAll($uid) {
  148. if (empty($uid)) {
  149. return false;
  150. }
  151. $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]);
  152. $result = Redis::hgetall($cacheKey);
  153. return $result ? $result : [];
  154. }
  155. /**
  156. * 删除最近阅读数据
  157. * @param $uid
  158. * @return mixed
  159. */
  160. public static function delRecentBooks($uid)
  161. {
  162. if (empty($uid)) {
  163. return false;
  164. }
  165. $cacheKey = Utils::getCacheKey('user.recent_books', [$uid]);
  166. return Redis::del($cacheKey);
  167. }
  168. /**
  169. * 设置当前阅读章节
  170. * @param $uid
  171. * @param $data
  172. * @return false
  173. */
  174. public static function setCurrentChapter($uid, $data) {
  175. if (empty($uid)) {
  176. return false;
  177. }
  178. $cacheKey = Utils::getCacheKey('user.current_chapter', [$uid]);
  179. return Redis::set($cacheKey, json_encode($data, JSON_UNESCAPED_UNICODE));
  180. }
  181. /**
  182. * 获取当前阅读章节
  183. * @param $uid
  184. * @return array|false|mixed
  185. */
  186. public static function getCurrentChapter($uid) {
  187. if (empty($uid)) {
  188. return false;
  189. }
  190. $cacheKey = Utils::getCacheKey('user.current_chapter', [$uid]);
  191. $result = Redis::get($cacheKey);
  192. return $result ? json_decode($result, true) : [];
  193. }
  194. /**
  195. * 删除当前阅读章节
  196. * @param $uid
  197. * @return mixed
  198. */
  199. public static function delCurrentChapter($uid)
  200. {
  201. if (empty($uid)) {
  202. return false;
  203. }
  204. $cacheKey = Utils::getCacheKey('user.current_chapter', [$uid]);
  205. return Redis::del($cacheKey);
  206. }
  207. /**
  208. * 设置今日好书分享数
  209. * @param $uid
  210. * @return bool
  211. */
  212. public static function setTodayShareNum($uid) {
  213. if (empty($uid)) {
  214. return false;
  215. }
  216. $cacheKey = Utils::getCacheKey('user.share_num', [$uid]);
  217. if (Redis::exists($cacheKey)) {
  218. Redis::incr($cacheKey);
  219. }else {
  220. $expire_at = strtotime(date('Y-m-d 23:59:59')) - time();
  221. Redis::setex($cacheKey, $expire_at, 1);
  222. }
  223. return true;
  224. }
  225. /**
  226. * 获取今日好书分享数
  227. * @param $uid
  228. * @return int
  229. */
  230. public static function getTodayShareNum($uid) {
  231. if (empty($uid)) {
  232. return 0;
  233. }
  234. $cacheKey = Utils::getCacheKey('user.share_num', [$uid]);
  235. $share_num = Redis::get($cacheKey);
  236. return $share_num ? $share_num : 0;
  237. }
  238. }