redis.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. 'use strict';
  2. var ready = require('ready');
  3. var redisLib = require('../libs/redis');
  4. var redisClient = redisLib.redisClient;
  5. /**
  6. * im独用redis client
  7. * @type {redisCo}
  8. */
  9. var redisCo = redisLib.redisCo;
  10. var sha_map = redisLib.sha_map; // redis锁相关函数
  11. /**
  12. * 连接旧javaredis库使用的
  13. * @type {javaRedisCo}
  14. */
  15. var javaRedisCo = redisLib.javaRedisCo;
  16. var C = require('../config');
  17. var F = require('../common/function');
  18. var _ = require('underscore');
  19. _.str = require('underscore.string');
  20. _.v = require('validator');
  21. var co = require('co');
  22. function redisMgr(app, common_mgr) {
  23. var model_map = app.model_mgr.model_map;
  24. var mgr_map = common_mgr.mgr_map;
  25. var that = this;
  26. that.boot_time = F.timestamp();
  27. var dbTimerPrefix = F.isNull(C.dbTimerPrefix) ? "" : C.dbTimerPrefix;
  28. this.default_redis_ttl = 15;//redis lock default 15s
  29. this.process_id = _.str.sprintf("%s:%s", C.inner_host, app.port);
  30. this.zrangebyscorestore = function* (sort_set_key, tmp_set_key, option_min, option_max) {
  31. redisClient.EVALSHA(sha_map.zrangebyscorestore_sha, 2, sort_set_key, tmp_set_key, option_min, option_max);
  32. }
  33. this.mysinter = function* (table,key_array,range_key_array,sort_field,isdesc,limit_first,limit_second) {
  34. //F.addDebugLogs(["###mysinter",table,key_array,range_key_array,sort_field,isdesc,limit_first,limit_second]);
  35. key_array = JSON.stringify(key_array);
  36. range_key_array = JSON.stringify(range_key_array);
  37. let res = yield redisCo.EVALSHA([sha_map.mysinter_sha,1,'{rk}nouse',table,key_array,range_key_array,sort_field,isdesc,limit_first,limit_second]);
  38. return JSON.parse(res)
  39. }
  40. this.getLock = function* (key, timeout = 20) { // 等待时间 尽量比default_redis_ttl大
  41. let wait_time = 0;
  42. key = '{lo}'+key;
  43. let pid = '{lo}'+that.process_id;
  44. while (wait_time < timeout * 1000) {
  45. let lock_time = new Date().getTime().toString(); // lua 整形范围有限
  46. let get = yield redisCo.EVALSHA([sha_map.lock_sha, 2, key, pid, JSON.stringify({
  47. "time": lock_time,
  48. "pid": pid
  49. }), this.default_redis_ttl]);
  50. if (get) return {
  51. "suc": 1,
  52. "time": lock_time,
  53. "key": key
  54. };
  55. yield F.sleep(125);
  56. wait_time += 125;
  57. }
  58. return {
  59. "suc": 0
  60. };
  61. }
  62. this.releaseLock = function* (lock) {
  63. if (F.isNull(lock) || F.isNull(lock.suc) || F.isNull(lock.time) || F.isNull(lock.key) || lock.suc == 0) return 0;
  64. lock.pid = '{lo}'+that.process_id;
  65. //F.addErrLogs(["releaseLock:", JSON.stringify(lock)]);
  66. let res = yield redisCo.EVALSHA([sha_map.unlock_sha, 2, lock.key, lock.pid, JSON.stringify(lock)]);
  67. lock.suc = null; // 保证锁只释放一次
  68. console.log("releaseLock res:", res);
  69. return res
  70. }
  71. /**
  72. * 获取全局唯一id 但是会过期重置的 默认过期10秒
  73. * @param key
  74. */
  75. this.getNextExpireId = function* (key, expire = 10) {
  76. var next_expire_id_key = C.redisPre.next_expire_id_key;
  77. next_expire_id_key = _.str.sprintf(next_expire_id_key, key);
  78. var next_id = yield redisCo.INCR(next_expire_id_key);
  79. yield redisCo.expire(next_expire_id_key, expire);
  80. return next_id;
  81. }
  82. this.getNextReqId = function* (uniid) {
  83. var key = C.redisPre.pack_req_id_key;
  84. var next_id = yield redisCo.HINCRBY(key, uniid, 1);
  85. next_id = parseInt(next_id);
  86. if (next_id > 9999999999999 && next_id % 100000 == 0) {
  87. yield redisCo.hmset(key, uniid, 1);
  88. }
  89. return next_id;
  90. };
  91. this.delPackNextId = function* (uniid) {
  92. var key = C.redisPre.pack_req_id_key;
  93. yield redisCo.hdel(key, uniid);
  94. };
  95. /**
  96. * 如果服务器同时支持多域名 用于记录用户登录时到底用哪个域名访问 以便后面根据域名获取不同图片服务器
  97. * @param token
  98. * @param host
  99. */
  100. this.setRealHost = function* (token, host) {
  101. var uniid_realip_map_key = C.redisPre.uniid_realip_map_key;
  102. uniid_realip_map_key = _.str.sprintf(uniid_realip_map_key, token);
  103. yield redisCo.set(uniid_realip_map_key, host);
  104. yield redisCo.expire(uniid_realip_map_key, C.session.ttl);
  105. };
  106. /*
  107. * 获取图片真实地址
  108. */
  109. this.getImagePrefix = function* (uniid, get = true) {
  110. if (F.isNull(uniid)) F.throwErrCode(10018);
  111. var uniid_realip_map_key = C.redisPre.uniid_realip_map_key;
  112. uniid_realip_map_key = _.str.sprintf(uniid_realip_map_key, uniid.split("__")[0]); // uniid = token_login时间戳
  113. yield redisCo.expire(uniid_realip_map_key, C.session.ttl);
  114. if (get) return yield redisCo.get(uniid_realip_map_key);
  115. };
  116. this.setHttpProtoc = function* (uniid, is_https) {
  117. if (F.isNull(is_https)) is_https = '';
  118. var uniid_https_map_key = C.redisPre.uniid_https_map_key;
  119. uniid_https_map_key = _.str.sprintf(uniid_https_map_key, uniid);
  120. yield redisCo.set(uniid_https_map_key, is_https);
  121. yield redisCo.expire(uniid_https_map_key, C.session.ttl);
  122. };
  123. /*
  124. * 获取是否https
  125. */
  126. this.getHttpProtoc = function* (uniid, get = true) {
  127. if (F.isNull(uniid)) F.throwErrCode(10018);
  128. var uniid_https_map_key = C.redisPre.uniid_https_map_key;
  129. uniid_https_map_key = _.str.sprintf(uniid_https_map_key, uniid.split("__")[0]); // uniid = token_login时间戳
  130. yield redisCo.expire(uniid_https_map_key, C.session.ttl);
  131. if (get) return yield redisCo.get(uniid_https_map_key);
  132. };
  133. this.setTimerId = function* (timerid) {
  134. var timer_id_prefix = C.redisPre.timer_id_prefix;
  135. timer_id_prefix = _.str.sprintf(timer_id_prefix, timerid);
  136. yield redisCo.set(timer_id_prefix, 1);
  137. };
  138. this.getTimerId = function* (timerid) {
  139. var timer_id_prefix = C.redisPre.timer_id_prefix;
  140. timer_id_prefix = _.str.sprintf(timer_id_prefix, timerid);
  141. return yield redisCo.get(timer_id_prefix);
  142. };
  143. this.delTimerId = function* (timerid) {
  144. var timer_id_prefix = C.redisPre.timer_id_prefix;
  145. timer_id_prefix = _.str.sprintf(timer_id_prefix, timerid);
  146. return yield redisCo.del(timer_id_prefix);
  147. };
  148. this.addJobToRedis = function* (next_exe_timestamp, jobid) {
  149. var job_prefix = dbTimerPrefix + C.redisPre.job_prefix;
  150. var msql_c = C.mysqlServers[0];
  151. job_prefix = _.str.vsprintf(job_prefix, [msql_c.host, msql_c.port]);
  152. yield redisCo.ZADD([job_prefix, next_exe_timestamp, jobid]);
  153. };
  154. this.checkJobs = function* (cur_time) {
  155. var job_prefix = dbTimerPrefix + C.redisPre.job_prefix;
  156. var msql_c = C.mysqlServers[0];
  157. job_prefix = _.str.vsprintf(job_prefix, [msql_c.host, msql_c.port]);
  158. var del_job_list = yield redisCo.zrangebyscore([job_prefix, 0, cur_time - 30 * 1000]);
  159. for (let i = 0; i < del_job_list.length; i++) {
  160. let job_id = del_job_list[i];
  161. redisClient.zrem(job_prefix, job_id); // 删除半分钟之前过期的定时任务
  162. }
  163. var job_count = yield redisCo.zcount([job_prefix, 0, cur_time]);
  164. return job_count;
  165. };
  166. this.delJob = function* (jobid) {
  167. var job_prefix = dbTimerPrefix + C.redisPre.job_prefix;
  168. var msql_c = C.mysqlServers[0];
  169. job_prefix = _.str.vsprintf(job_prefix, [msql_c.host, msql_c.port]);
  170. yield redisCo.zrem([job_prefix, jobid]);
  171. };
  172. this.setJobStopFlag = function* () {
  173. var job_stop_flag = C.redisPre.job_stop_flag;
  174. yield redisCo.set(job_stop_flag, 1);
  175. };
  176. this.getJobStopFlag = function* () {
  177. var job_stop_flag = C.redisPre.job_stop_flag;
  178. return yield redisCo.get(job_stop_flag);
  179. };
  180. this.delJobStopFlag = function* () {
  181. var job_stop_flag = C.redisPre.job_stop_flag;
  182. return yield redisCo.del(job_stop_flag);
  183. };
  184. this.setSendReqCbData = function* (seq_id, cb_data) {
  185. var send_req_cb_data = C.redisPre.send_req_cb_data;
  186. send_req_cb_data = _.str.sprintf(send_req_cb_data, seq_id);
  187. var cb_data_str = JSON.stringify(cb_data);
  188. yield redisCo.set(send_req_cb_data, cb_data_str);
  189. yield redisCo.expire(send_req_cb_data, 60);
  190. }
  191. this.getSendReqCbData = function* (seq_id) {
  192. var send_req_cb_data = C.redisPre.send_req_cb_data;
  193. send_req_cb_data = _.str.sprintf(send_req_cb_data, seq_id);
  194. var cb_data_str = yield redisCo.get(send_req_cb_data);
  195. yield redisCo.del(send_req_cb_data);
  196. return JSON.parse(cb_data_str);
  197. }
  198. this.setnx = function* (key, expire = 172800) {
  199. return yield redisCo.EVALSHA([sha_map.setnxex_sha, 1, key, 1, expire]);
  200. //let ttl = yield redisCo.ttl(key);
  201. //if (ttl == -1) yield redisCo.expire(key, expire);
  202. //let lock = yield redisCo.setnx(key, 1);
  203. //if (lock) yield redisCo.expire(key, expire);
  204. //return lock;
  205. }
  206. this.expire = function* (key, time) {
  207. return yield redisCo.expire(key, time);
  208. }
  209. this.set = function* (key, value) {
  210. return yield redisCo.set(key, value);
  211. };
  212. this.get = function* (key) {
  213. return yield redisCo.get(key);
  214. };
  215. this.del = function* (key) {
  216. return yield redisCo.del(key);
  217. };
  218. this.getRedisCo = function () {
  219. return redisCo;
  220. };
  221. this.zscore = function* (key, item) {
  222. return yield redisCo.zscore([key, item]);
  223. };
  224. this.zcard = function* (key) {
  225. return yield redisCo.zcard([key]);
  226. };
  227. this.zrange = function* (key, min = 0, max = -1, option = null) {
  228. if (option == null) return yield redisCo.zrange(key, min, max);
  229. else if (option == 'WITHSCORES') return yield redisCo.zrange([key, min, max, 'WITHSCORES']);
  230. //TODO 实现limit
  231. return yield redisCo.zrange(key, min, max);
  232. };
  233. this.hincrby = function* (key, field, increment = 1) {
  234. return yield redisCo.HINCRBY(key, field, increment);
  235. };
  236. this.ttl = function* (key) {
  237. return yield redisCo.ttl(key);
  238. };
  239. this.hmset = function* (key, data, expire = null) {
  240. let insert_data = [key];
  241. insert_data.push.apply(insert_data, data);
  242. let res = yield redisCo.hmset(insert_data);
  243. if (!F.isNull(expire)) {
  244. res = yield redisCo.expire(key, expire);
  245. }
  246. return res;
  247. };
  248. this.hmget = function* (key, fields) {
  249. let fields_array = [key];
  250. fields_array.push.apply(fields_array, fields);
  251. return yield redisCo.hmget(fields_array);
  252. };
  253. this.hkeys = function* (key) {
  254. return yield redisCo.hkeys(key);
  255. };
  256. this.hgetall = function* (key) {
  257. return yield redisCo.hgetall(key);
  258. };
  259. this.sadd = function* (key, value, expire = null) {
  260. let fields_array = [key];
  261. if (Array.isArray(value)) {
  262. fields_array.push.apply(fields_array, value);
  263. } else {
  264. fields_array.push(value);
  265. }
  266. let res = yield redisCo.sadd(fields_array);
  267. if (!F.isNull(expire)) {
  268. yield redisCo.expire(key, expire);
  269. }
  270. return res;
  271. };
  272. /**
  273. * 返回集合中包含的所有元素
  274. * @param key
  275. * @returns {*}
  276. */
  277. this.smembers = function* (key) {
  278. return yield redisCo.smembers(key);
  279. }
  280. /**
  281. * 返回那些存在于第一个集合,但不存在于其他集合的元素(差集)
  282. * @param key
  283. * @returns {*}
  284. */
  285. this.sdiff = function* (key) {
  286. return yield redisCo.sdiff(key);
  287. }
  288. this.sinter = function* (keys) {
  289. return yield redisCo.sinter(keys);
  290. }
  291. this.exists = function* (key) {
  292. return yield redisCo.exists(key);
  293. }
  294. this.zadd = function* (key, value, score, expire = null) {
  295. F.addOtherLogs("imrw/imrw", ["addRoomHistory zadd",value]);
  296. let res = yield redisCo.ZADD([key, score, value]);
  297. if (!F.isNull(expire)) {
  298. res = yield redisCo.expire(key, expire);
  299. }
  300. return res;
  301. }
  302. this.zrangebyscore = function* (key, min, max) {
  303. return yield redisCo.zrangebyscore([key, min, max]);
  304. }
  305. this.zrem = function* (key, value) {
  306. let fields_array = [key];
  307. if (Array.isArray(value)) {
  308. fields_array.push.apply(fields_array, value);
  309. } else {
  310. fields_array.push(value);
  311. }
  312. return yield redisCo.zrem(fields_array);
  313. };
  314. this.zrank = function* (key, member) {
  315. return yield redisCo.ZRANK([key, member]);
  316. }
  317. this.lpush = function* (key, value, expire = null) {
  318. let fields_array = [key];
  319. if (Array.isArray(value)) {
  320. fields_array.push.apply(fields_array, value);
  321. } else {
  322. fields_array.push(value);
  323. }
  324. let res = yield redisCo.lpush(fields_array);
  325. if (!F.isNull(expire)) {
  326. yield redisCo.expire(key, expire);
  327. }
  328. return res;
  329. }
  330. /* 查询用户登录记录IP信息 */
  331. this.getUserLoginIpInfo = function* (userid) {
  332. let key = _.str.sprintf(C.redisPre.userid_login_ip_info_key, userid);
  333. let value = yield redisCo.get(key);
  334. return F.isNull(value) ? '' : JSON.parse(value);
  335. }
  336. this.lrem = function* (key, value, count = 1) {
  337. return yield redisCo.lrem(key, count, value);
  338. }
  339. this.llen = function* (key) {
  340. return yield redisCo.llen(key);
  341. }
  342. /* 设置用户登录记录IP信息 */
  343. this.setUserLoginIpInfo = function* (userid, ip, user_agent, expire = C.session.ttl) {
  344. let key = _.str.sprintf(C.redisPre.userid_login_ip_info_key, userid);
  345. let value = {'ip': ip, 'user_agent': user_agent};
  346. yield redisCo.set(key, JSON.stringify(value));
  347. yield redisCo.expire(key, expire);
  348. }
  349. /****
  350. * 以下为聚效业务逻辑 以上业务逻辑不再使用 但保留先
  351. */
  352. // this.addUniidToBigRoom = function* (uniid) {
  353. // var userid_uni_map_key = C.redisPre.userid_uni_map_key;
  354. // userid_uni_map_key = _.str.sprintf(userid_uni_map_key, "BigRoom");
  355. // var insert_time = F.timestamp();
  356. // yield redisCo.ZADD([userid_uni_map_key, insert_time, uniid]);
  357. // };
  358. //
  359. // this.delUniidFromBigRoom = function* (uniid) {
  360. // var userid_uni_map_key = C.redisPre.userid_uni_map_key;
  361. // userid_uni_map_key = _.str.sprintf(userid_uni_map_key, "BigRoom");
  362. // yield redisCo.zrem([userid_uni_map_key, uniid]);
  363. // };
  364. // this.getAllSockets = function* () {
  365. // var userid_uni_map_key = C.redisPre.userid_uni_map_key;
  366. // userid_uni_map_key = _.str.sprintf(userid_uni_map_key, "BigRoom");
  367. // return yield redisCo.zrange(userid_uni_map_key, 0, -1);
  368. // };
  369. this.setRoomInfo = function* (room_id, room_info) {
  370. let key = _.str.sprintf(C.redisPre.room_info_key, room_id);
  371. return yield redisCo.set(key, JSON.stringify(room_info));
  372. };
  373. this.getRoomInfo = function* (room_id) {
  374. let room_uid = yield redisCo.hget(C.redisPre.room_room_id, room_id);
  375. if (F.isNull(room_uid)) {
  376. return null;
  377. }
  378. // let room_id_key = JSON.parse(roomUid);
  379. // let key = _.str.sprintf(C.redisPre.uid_type_map_room_info, room_id_key.type);
  380. let res = yield redisCo.hget(C.redisPre.uid_type_map_room_info,room_uid);
  381. return F.isNull(res) ? res : JSON.parse(res);
  382. };
  383. this.getRoomAudioChannel = function* (room_id) {
  384. let audioChannel = yield redisCo.hget(C.redisPre.room_audio_channel_key, room_id);
  385. if (!F.isNull(audioChannel)) {
  386. return Number(audioChannel);
  387. }
  388. audioChannel = yield redisCo.get(C.redisPre.default_audio_channel_key);
  389. return F.isNull(audioChannel) ? 1 : Number(audioChannel);
  390. };
  391. this.setRoomAudioChannel = function* (audio_channel, room_id) {
  392. if (F.isNull(room_id)) {
  393. yield redisCo.set(C.redisPre.default_audio_channel_key, audio_channel);
  394. } else {
  395. yield redisCo.hset(C.redisPre.room_audio_channel_key, room_id, audio_channel);
  396. }
  397. };
  398. // 设置房间黑名单
  399. this.setRoomBlacklist = function* (room_id, uid, black_info) {
  400. let key = _.str.sprintf(C.redisPre.room_blacklist_key, room_id);
  401. return yield redisCo.hset(key, uid, JSON.stringify(black_info));
  402. };
  403. // 删除房间黑名单
  404. this.delRoomBlacklist = function* (room_id, uid) {
  405. let key = _.str.sprintf(C.redisPre.room_blacklist_key, room_id);
  406. return yield redisCo.hdel(key, uid);
  407. };
  408. // 获取房间黑名单
  409. this.getRoomBlacklist = function* (room_id) {
  410. let key = _.str.sprintf(C.redisPre.room_blacklist_key, room_id);
  411. let res = yield redisCo.hkeys(key);
  412. return res;
  413. };
  414. // 获取用户是否在房间黑名单
  415. this.isBlacklist = function* (room_id, uid) {
  416. let key = _.str.sprintf(C.redisPre.room_blacklist_key, room_id);
  417. let res = yield redisCo.hget(key, uid);
  418. return !F.isNull(res);
  419. };
  420. // 设置房间禁言
  421. this.setRoomMute = function* (room_id, uid, mute_info) {
  422. let key = _.str.sprintf(C.redisPre.room_mute_key, room_id);
  423. return yield redisCo.hset(key, uid, JSON.stringify(mute_info));
  424. };
  425. // 删除房间禁言
  426. this.delRoomMute = function* (room_id, uid) {
  427. let key = _.str.sprintf(C.redisPre.room_mute_key, room_id);
  428. return yield redisCo.hdel(key, uid);
  429. };
  430. // 获取房间禁言
  431. this.getRoomMute = function* (room_id) {
  432. let key = _.str.sprintf(C.redisPre.room_mute_key, room_id);
  433. let res = yield redisCo.hkeys(key);
  434. return res;
  435. };
  436. // 获取用户是否在房间禁言
  437. this.isMute = function* (room_id, uid) {
  438. let key = _.str.sprintf(C.redisPre.room_mute_key, room_id);
  439. let res = yield redisCo.hget(key, uid);
  440. return !F.isNull(res);
  441. };
  442. // ==================================管理员==================================//
  443. // 设置房间管理员
  444. this.setRoomManager = function* (room_id, uid) {
  445. let key = _.str.sprintf(C.redisPre.room_manager_key, room_id);
  446. var insert_time = F.timestamp();
  447. yield redisCo.ZADD([key, insert_time, uid]);
  448. };
  449. // 删除房间管理员
  450. this.delRoomManager = function* (room_id, uid) {
  451. let key = _.str.sprintf(C.redisPre.room_manager_key, room_id);
  452. yield redisCo.zrem([key, uid]);
  453. };
  454. // 获取房间管理员
  455. this.getRoomManager = function* (room_id, uid) {
  456. let key = _.str.sprintf(C.redisPre.room_manager_key, room_id);
  457. let score = yield redisCo.ZSCORE([key, uid]);
  458. return score;
  459. };
  460. this.getAllRoomManagers = function* (room_id) {
  461. let key = _.str.sprintf(C.redisPre.room_manager_key, room_id);
  462. return yield this.zrange(key, 0, -1);
  463. };
  464. // 是否房间管理员
  465. this.getRole = function* (room_id, uid) {
  466. let key = _.str.sprintf(C.redisPre.room_manager_key, room_id);
  467. let score = yield redisCo.ZSCORE([key, uid]);
  468. return F.isNull(score) ? 0 : 1;
  469. };
  470. // ==================================管理员==================================//
  471. // 记录用户基础信息
  472. this.setUserInfo = function* (uid, user_info) {
  473. let key = _.str.sprintf(C.redisPre.user_info_key, uid);
  474. return yield redisCo.set(key, JSON.stringify(user_info));
  475. };
  476. this.getUserInfo = function* (uid) {
  477. let key = _.str.sprintf(C.redisPre.user_info_key, uid);
  478. let res = yield redisCo.get(key);
  479. return F.isNull(res) ? res : JSON.parse(res);
  480. };
  481. /******* END ******/
  482. // 记录用户socket列表
  483. this.addSocketToUser = function* (uid, socketid) {
  484. var user_map_socket_key = C.redisPre.user_map_socket_key;
  485. user_map_socket_key = _.str.sprintf(user_map_socket_key, uid);
  486. var insert_time = F.timestamp();
  487. yield redisCo.ZADD([user_map_socket_key, insert_time, socketid]);
  488. };
  489. this.getUserSocketList = function* (uid) {
  490. var user_map_socket_key = C.redisPre.user_map_socket_key;
  491. user_map_socket_key = _.str.sprintf(user_map_socket_key, uid);
  492. return yield this.zrange(user_map_socket_key, 0, -1);
  493. };
  494. this.delUserSocketList = function* (uid, socketid) {
  495. var user_map_socket_key = C.redisPre.user_map_socket_key;
  496. user_map_socket_key = _.str.sprintf(user_map_socket_key, uid);
  497. yield redisCo.zrem([user_map_socket_key, socketid]);
  498. };
  499. /******* END ******/
  500. // 存某个房间用户列表
  501. this.addUidToRoom = function* (room_id, uid) {
  502. var roomid_map_uid_key = C.redisPre.roomid_map_uid_key;
  503. roomid_map_uid_key = _.str.sprintf(roomid_map_uid_key, room_id);
  504. var insert_time = F.timestamp();
  505. //如果是管理员时间戳乘以2
  506. let is_admin = yield this.getRole(room_id, uid);
  507. if (is_admin) {
  508. insert_time = insert_time * 2;
  509. }
  510. yield redisCo.ZADD([roomid_map_uid_key, insert_time, uid]);
  511. };
  512. this.getRoomUserList = function* (room_id) {
  513. var roomid_map_uid_key = _.str.sprintf(C.redisPre.roomid_map_uid_key, room_id);
  514. return yield redisCo.zrange(roomid_map_uid_key, 0, -1)
  515. };
  516. /**根据房间id获取成员分页数据*/
  517. this.getRoomUserPage = function* (room_id, start, limit) {
  518. let roomid_map_uid_key = _.str.sprintf(C.redisPre.roomid_map_uid_key, room_id);
  519. return yield redisCo.zrange(roomid_map_uid_key, start, limit);
  520. };
  521. this.getRoomUserCount = function* (room_id) {
  522. let roomid_map_uid_key = _.str.sprintf(C.redisPre.roomid_map_uid_key, room_id);
  523. let room_count = yield redisCo.zcount([roomid_map_uid_key, '-inf', '+inf']);
  524. return F.isNull(room_count) ? 0 : room_count;
  525. };
  526. this.delRoomUserList = function* (room_id, uid) {
  527. let roomid_map_uid_key = _.str.sprintf(C.redisPre.roomid_map_uid_key, room_id);
  528. F.log("err:", "key=%s", [roomid_map_uid_key]);
  529. yield redisCo.zrem([roomid_map_uid_key, uid]);
  530. };
  531. /******* END ******/
  532. // 存某个房间socket列表
  533. this.addSocketToRoom = function* (room_id, socketId) {
  534. let roomid_map_socket_key = _.str.sprintf(C.redisPre.roomid_map_socket_key, room_id);
  535. let insert_time = F.timestamp();
  536. co(redisCo.zremrangebyscore([roomid_map_socket_key, 0, that.boot_time- 10*60]));
  537. yield redisCo.ZADD([roomid_map_socket_key, insert_time, socketId]);
  538. };
  539. this.getRoomSocketList = function* (room_id) {
  540. var roomid_map_socket_key = C.redisPre.roomid_map_socket_key;
  541. roomid_map_socket_key = _.str.sprintf(roomid_map_socket_key, room_id);
  542. return yield this.zrange(roomid_map_socket_key, 0, -1);
  543. };
  544. this.getRoomSocketId = function* (room_id, socket_id) {
  545. let key = _.str.sprintf(C.redisPre.roomid_map_socket_key, room_id);
  546. let score = yield redisCo.ZSCORE([key, socket_id]);
  547. return score;
  548. };
  549. this.delRoomSocketList = function* (room_id, socketId) {
  550. var roomid_map_socket_key = C.redisPre.roomid_map_socket_key;
  551. roomid_map_socket_key = _.str.sprintf(roomid_map_socket_key, room_id);
  552. yield redisCo.zrem([roomid_map_socket_key, socketId]);
  553. };
  554. /******* END ******/
  555. // 反向记录socket在哪些房间
  556. this.addRoomidToSocket = function* (socketId, room_id) {
  557. var socket_map_room_key = C.redisPre.socket_map_room_key;
  558. socket_map_room_key = _.str.sprintf(socket_map_room_key, socketId);
  559. var insert_time = F.timestamp();
  560. yield redisCo.ZADD([socket_map_room_key, insert_time, room_id]);
  561. };
  562. this.getSocketRoomList = function* (socketId) {
  563. let socket_map_room_key = _.str.sprintf(C.redisPre.socket_map_room_key, socketId);
  564. return yield this.zrange(socket_map_room_key, 0, -1);
  565. };
  566. this.delSocketRoomList = function* (socketId, room_id) {
  567. F.addDebugLogs(["delSocketRoomList:", socketId, room_id]);
  568. var socket_map_room_key = C.redisPre.socket_map_room_key;
  569. socket_map_room_key = _.str.sprintf(socket_map_room_key, socketId);
  570. yield redisCo.zrem([socket_map_room_key, room_id]);
  571. };
  572. /******* END ******/
  573. // 存某个用户+某个房间的socket列表
  574. this.addSocketToUidRoom = function* (uid, room_id, socketId) {
  575. var uid_room_map_socket_key = C.redisPre.uid_room_map_socket_key;
  576. uid_room_map_socket_key = _.str.vsprintf(uid_room_map_socket_key, [uid, room_id]);
  577. co(redisCo.zremrangebyscore([uid_room_map_socket_key, 0, that.boot_time- 10*60]));
  578. var insert_time = F.timestamp();
  579. yield redisCo.ZADD([uid_room_map_socket_key, insert_time, socketId]);
  580. };
  581. this.getSocketListByUidAndRoomid = function* (uid, room_id) {
  582. var uid_room_map_socket_key = C.redisPre.uid_room_map_socket_key;
  583. uid_room_map_socket_key = _.str.vsprintf(uid_room_map_socket_key, [uid, room_id]);
  584. return yield this.zrange(uid_room_map_socket_key, 0, -1);
  585. }
  586. this.delUidRoomSocketList = function* (uid, room_id, socketId) {
  587. var uid_room_map_socket_key = C.redisPre.uid_room_map_socket_key;
  588. uid_room_map_socket_key = _.str.vsprintf(uid_room_map_socket_key, [uid, room_id]);
  589. yield redisCo.zrem([uid_room_map_socket_key, socketId]);
  590. };
  591. /******* END ******/
  592. //房间队列成员映射
  593. this.addMemberToQueue = function* (room_id, key, memberid) {
  594. if (parseInt(key) < -1 || parseInt(key) > 7) F.throwErr("queue key is wrong!");
  595. var room_map_queue_mem_key = C.redisPre.room_map_queue_mem_key;
  596. room_map_queue_mem_key = _.str.vsprintf(room_map_queue_mem_key, [room_id]);
  597. yield redisCo.hmset(room_map_queue_mem_key, key, memberid);
  598. };
  599. this.getAllQueueMember = function* (room_id) {
  600. var room_map_queue_mem_key = C.redisPre.room_map_queue_mem_key;
  601. room_map_queue_mem_key = _.str.vsprintf(room_map_queue_mem_key, [room_id]);
  602. let res = yield redisCo.hmget(room_map_queue_mem_key, [-1, 0, 1, 2, 3, 4, 5, 6, 7]);
  603. if (F.isNull(res)) {
  604. return [null, null, null, null, null, null, null, null, null];
  605. }
  606. return res;
  607. };
  608. this.checkInQueue = function* (room_id, uid) {
  609. let que_list = yield this.getAllQueueMember(room_id);
  610. F.addDebugLogs(["###getAllQueueMember:",que_list]);
  611. for (let i = 0; i < que_list.length; i++) {
  612. let mem_id = que_list[i];
  613. if (uid == mem_id) return true;
  614. }
  615. return false;
  616. }
  617. this.delQueueMember = function* (room_id, key) {
  618. var room_map_queue_mem_key = C.redisPre.room_map_queue_mem_key;
  619. room_map_queue_mem_key = _.str.vsprintf(room_map_queue_mem_key, [room_id]);
  620. yield redisCo.hdel(room_map_queue_mem_key, key);
  621. };
  622. /******* END ******/
  623. // 统计房间在线人数
  624. this.addRoomOnlineNum = function* (room_id) {
  625. let online_num = yield that.getRoomUserCount(room_id);
  626. let key = C.redisPre.room_live_sortedset_key;
  627. // zincrby key increment member
  628. yield redisCo.ZADD([key, online_num, room_id]);
  629. return online_num;
  630. };
  631. this.getRoomOnlineNum = function* (room_id) {
  632. let key = C.redisPre.room_live_sortedset_key;
  633. let online_num = yield redisCo.zscore([key, room_id]);
  634. return Number(online_num);
  635. };
  636. this.getRoomIdsByOnlineNum = function* (online_num = 3) {
  637. let key = C.redisPre.room_live_sortedset_key;
  638. return yield redisCo.zrangebyscore([key, online_num, '+inf']);
  639. };
  640. /******* END ******/
  641. // 往公聊大厅添加一个socket
  642. this.addSocketToPublicRoom = function* (room_id, socket_id, uid) {
  643. let insert_time = F.timestamp();
  644. let public_roomid_map_socketid_zset_key = _.str.sprintf(C.redisPre.public_roomid_map_socketid_zset_key, room_id);
  645. co(redisCo.zremrangebyscore([public_roomid_map_socketid_zset_key, 0, that.boot_time- 10*60]));
  646. yield redisCo.ZADD([public_roomid_map_socketid_zset_key, insert_time, socket_id]);
  647. let public_roomid_map_uid_zset_key = _.str.sprintf(C.redisPre.public_roomid_map_uid_zset_key, room_id);
  648. yield redisCo.ZADD([public_roomid_map_uid_zset_key, insert_time, uid]);
  649. let public_socketid_map_roomid_zset_key = _.str.sprintf(C.redisPre.public_socketid_map_roomid_zset_key, socket_id);
  650. yield redisCo.ZADD([public_socketid_map_roomid_zset_key, insert_time, room_id]);
  651. };
  652. /**
  653. * 获取公聊大厅的socket列表
  654. * @param room_id
  655. * @returns {*}
  656. */
  657. this.getPublicRoomSocketList = function* (room_id) {
  658. let key = _.str.sprintf(C.redisPre.public_roomid_map_socketid_zset_key, room_id);
  659. return yield this.zrange(key, 0, -1);
  660. };
  661. this.getRecentPublicRoomUidList = function* (room_id) {
  662. let key = _.str.sprintf(C.redisPre.public_roomid_map_uid_zset_key, room_id);
  663. return yield redisCo.ZREVRANGE([key, 0, 5]);
  664. };
  665. this.getPublicRoomUserCount = function* (room_id) {
  666. let insert_time = F.timestamp();
  667. let key = _.str.sprintf(C.redisPre.public_roomid_map_uid_zset_key, room_id);
  668. return yield redisCo.ZCOUNT([key, 0, insert_time]);
  669. };
  670. /**
  671. * 根据当前的socket_id获取所在房间
  672. * @param socket_id
  673. * @returns {*}
  674. */
  675. this.getPublicRoomIdBySocketId = function* (socket_id) {
  676. let key = _.str.sprintf(C.redisPre.public_socketid_map_roomid_zset_key, socket_id);
  677. return yield this.zrange(key, 0, -1);
  678. };
  679. /**
  680. * 用户退出公聊大厅删除缓存
  681. * @param room_id
  682. * @param socket_id
  683. */
  684. this.delPublicRoomSocket = function* (uid, socket_id) {
  685. let key = _.str.sprintf(C.redisPre.public_socketid_map_roomid_zset_key, socket_id);
  686. let room_id_list = yield this.zrange(key, 0, -1);
  687. let public_socketid_map_roomid_zset_key = _.str.sprintf(C.redisPre.public_socketid_map_roomid_zset_key, socket_id);
  688. for (let i = 0; i < room_id_list.length; i++) {
  689. let public_roomid_map_socketid_zset_key = _.str.sprintf(C.redisPre.public_roomid_map_socketid_zset_key, room_id_list[i]);
  690. yield redisCo.zrem([public_roomid_map_socketid_zset_key, socket_id]);
  691. let public_roomid_map_uid_zset_key = _.str.sprintf(C.redisPre.public_roomid_map_uid_zset_key, room_id_list[i]);
  692. yield redisCo.zrem([public_roomid_map_uid_zset_key, uid]);
  693. yield redisCo.zrem([public_socketid_map_roomid_zset_key, room_id_list[i]]);
  694. }
  695. };
  696. /**
  697. * 保存公聊大厅的聊天历史记录
  698. * @param room_id
  699. * @param custom
  700. */
  701. this.addPublicRoomHistory = function* (room_id, custom) {
  702. let key = _.str.sprintf(C.redisPre.public_room_history_zset_key, room_id);
  703. let insert_time = F.timestamp();
  704. yield redisCo.ZADD([key, insert_time, JSON.stringify(custom)]);
  705. // 保存50条
  706. let his_count = yield redisCo.ZCOUNT([key, 0, insert_time]);
  707. if (Number(his_count) > 50) {
  708. yield redisCo.ZREMRANGEBYRANK([key, 0, Number(his_count) - 50 - 1]);
  709. }
  710. };
  711. this.getPublicRoomHisLis = function* (room_id) {
  712. let key = _.str.sprintf(C.redisPre.public_room_history_zset_key, room_id);
  713. return yield redisCo.ZRANGE([key, 0, -1]);
  714. };
  715. /**
  716. * 保存房间的聊天历史记录
  717. * @param room_id
  718. * @param custom
  719. */
  720. this.addRoomHistory = function* (room_id, custom) {
  721. let key = _.str.sprintf(C.redisPre.public_room_chat_history_zset_key, room_id);
  722. let insert_time = F.timestamp();
  723. yield redisCo.ZADD([key, insert_time, JSON.stringify(custom)]);
  724. // 保存50条
  725. let his_count = yield redisCo.ZCOUNT([key, 0, insert_time]);
  726. if (Number(his_count) > 50) {
  727. yield redisCo.ZREMRANGEBYRANK([key, 0, Number(his_count) - 50 - 1]);
  728. }
  729. };
  730. this.getRoomHisList = function* (room_id) {
  731. let key = _.str.sprintf(C.redisPre.public_room_chat_history_zset_key, room_id);
  732. return yield redisCo.ZRANGE([key, 0, -1]);
  733. };
  734. this.setStreamId = function* (uid,streamid) {
  735. let key = _.str.sprintf(C.redisPre.stream_uid_map_streamid_key, uid);
  736. yield redisCo.set(key, streamid);
  737. }
  738. this.getStreamId = function* (uid) {
  739. let key = _.str.sprintf(C.redisPre.stream_uid_map_streamid_key, uid);
  740. return yield redisCo.get(key);
  741. }
  742. this.setStreamSvrId = function* (uid,streamsvrid) {
  743. let key = _.str.sprintf(C.redisPre.stream_uid_map_streamsvrid_key, uid);
  744. yield redisCo.set(key, streamsvrid);
  745. }
  746. this.getStreamSvrId = function* (uid) {
  747. let key = _.str.sprintf(C.redisPre.stream_uid_map_streamsvrid_key, uid);
  748. return yield redisCo.get(key);
  749. }
  750. /******* END ******/
  751. this.getEnterRoomWhiteList = function* () {
  752. let value = yield redisCo.get(C.redisPre.enter_room_white_list);
  753. if (F.isNull(value)) return [];
  754. else {
  755. return value.split(",")
  756. }
  757. }
  758. }
  759. module.exports = redisMgr;