redis.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package fav
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/job/main/favorite/model"
  7. favmdl "go-common/app/service/main/favorite/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. )
  11. const (
  12. // _fid list fid => ([]model.Cover)
  13. _covers = "fcs_"
  14. _folderKey = "fi_%d_%d" // sortedset f_type_mid value:fid,score:ctime
  15. _oldRelationKey = "r_%d_%d_%d"
  16. _allRelationKey = "ar_%d_%d"
  17. _relationKey = "r_%d_%d" // sortedset r_mid_fid(mtime, oid)
  18. _relationOidsKey = "ro_%d_%d" // set ro_type_mid value:oids
  19. _cleanedKey = "rc_%d_%d" // hash key:rc_type_mid field:fid value:timestamp
  20. // key fb_mid/100000 offset => mid%100000
  21. // bit value 1 mean unfaved; bit value 0 mean faved
  22. _favedBit = "fb_%d_%d"
  23. _bucket = 100000
  24. )
  25. func favedBitKey(tp int8, mid int64) string {
  26. return fmt.Sprintf(_favedBit, tp, mid/_bucket)
  27. }
  28. // folderKey return a user folder key.
  29. func folderKey(tp int8, mid int64) string {
  30. return fmt.Sprintf(_folderKey, tp, mid)
  31. }
  32. // relationKey return folder relation key.
  33. func relationKey(mid, fid int64) string {
  34. return fmt.Sprintf(_relationKey, mid, fid)
  35. }
  36. // allRelationKey return folder relation key.
  37. func allRelationKey(mid, fid int64) string {
  38. return fmt.Sprintf(_allRelationKey, mid, fid)
  39. }
  40. // oldRelationKey return folder relation key.
  41. func oldRelationKey(typ int8, mid, fid int64) string {
  42. return fmt.Sprintf(_oldRelationKey, typ, mid, fid)
  43. }
  44. // relationOidsKey return a user oids key.
  45. func relationOidsKey(tp int8, mid int64) string {
  46. return fmt.Sprintf(_relationOidsKey, tp, mid)
  47. }
  48. // cleanKey return user whether cleaned key.
  49. func cleanedKey(tp int8, mid int64) string {
  50. return fmt.Sprintf(_cleanedKey, tp, mid)
  51. }
  52. // redisKey make key for redis by prefix and mid
  53. func coversKey(mid, fid int64) string {
  54. return fmt.Sprintf("%s%d_%d", _covers, mid, fid)
  55. }
  56. // PingRedis ping connection success.
  57. func (d *Dao) pingRedis(c context.Context) (err error) {
  58. conn := d.redis.Get(c)
  59. _, err = conn.Do("SET", "PING", "PONG")
  60. conn.Close()
  61. return
  62. }
  63. // DelNewCoverCache delete cover picture cache.
  64. func (d *Dao) DelNewCoverCache(c context.Context, mid, fid int64) (err error) {
  65. var (
  66. key = coversKey(mid, fid)
  67. conn = d.redis.Get(c)
  68. )
  69. defer conn.Close()
  70. if _, err = conn.Do("DEL", key); err != nil {
  71. log.Error("DEL %v failed error(%v)", key, err)
  72. }
  73. return
  74. }
  75. // SetUnFavedBit set unfaved user bit to 1
  76. func (d *Dao) SetUnFavedBit(c context.Context, tp int8, mid int64) (err error) {
  77. key := favedBitKey(tp, mid)
  78. offset := mid % _bucket
  79. conn := d.redis.Get(c)
  80. defer conn.Close()
  81. if _, err = conn.Do("SETBIT", key, offset, 1); err != nil {
  82. log.Error("conn.DO(SETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
  83. }
  84. return
  85. }
  86. // SetFavedBit set unfaved user bit to 0
  87. func (d *Dao) SetFavedBit(c context.Context, tp int8, mid int64) (err error) {
  88. key := favedBitKey(tp, mid)
  89. offset := mid % _bucket
  90. conn := d.redis.Get(c)
  91. defer conn.Close()
  92. if _, err = conn.Do("SETBIT", key, offset, 0); err != nil {
  93. log.Error("conn.DO(SETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
  94. }
  95. return
  96. }
  97. // ExpireAllRelations expire folder relations cache.
  98. func (d *Dao) ExpireAllRelations(c context.Context, mid, fid int64) (ok bool, err error) {
  99. key := allRelationKey(mid, fid)
  100. conn := d.redis.Get(c)
  101. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
  102. log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
  103. }
  104. conn.Close()
  105. return
  106. }
  107. // ExpireRelations expire folder relations cache.
  108. func (d *Dao) ExpireRelations(c context.Context, mid, fid int64) (ok bool, err error) {
  109. key := relationKey(mid, fid)
  110. conn := d.redis.Get(c)
  111. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
  112. log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
  113. }
  114. conn.Close()
  115. return
  116. }
  117. // FolderCache return a favorite folder from redis.
  118. func (d *Dao) FolderCache(c context.Context, tp int8, mid, fid int64) (folder *favmdl.Folder, err error) {
  119. var (
  120. value []byte
  121. key = folderKey(tp, mid)
  122. conn = d.redis.Get(c)
  123. )
  124. defer conn.Close()
  125. if value, err = redis.Bytes(conn.Do("HGET", key, fid)); err != nil {
  126. if err == redis.ErrNil {
  127. err = nil
  128. folder = nil
  129. } else {
  130. log.Error("conn.Do(HGET, %v, %v) error(%v)", key, fid, err)
  131. }
  132. return
  133. }
  134. folder = &favmdl.Folder{}
  135. if err = json.Unmarshal(value, folder); err != nil {
  136. log.Error("json.Unmarshal(%s) error(%v)", value, err)
  137. }
  138. return
  139. }
  140. // DefaultFolderCache return default favorite folder from redis.
  141. func (d *Dao) DefaultFolderCache(c context.Context, tp int8, mid int64) (folder *favmdl.Folder, err error) {
  142. var res map[int64]*favmdl.Folder
  143. if res, err = d.foldersCache(c, tp, mid); err != nil {
  144. return
  145. }
  146. if res == nil {
  147. return
  148. }
  149. for _, folder = range res {
  150. if folder.IsDefault() {
  151. return
  152. }
  153. }
  154. folder = nil
  155. return
  156. }
  157. // foldersCache return the user all folders from redis.
  158. func (d *Dao) foldersCache(c context.Context, tp int8, mid int64) (res map[int64]*favmdl.Folder, err error) {
  159. var (
  160. values map[string]string
  161. key = folderKey(tp, mid)
  162. conn = d.redis.Get(c)
  163. )
  164. defer conn.Close()
  165. if values, err = redis.StringMap(conn.Do("HGETALL", key)); err != nil {
  166. if err == redis.ErrNil {
  167. return nil, nil
  168. }
  169. log.Error("conn.Do(HGETALL %s) error(%v)", key, err)
  170. return
  171. }
  172. res = make(map[int64]*favmdl.Folder, len(res))
  173. for _, data := range values {
  174. folder := &favmdl.Folder{}
  175. if err = json.Unmarshal([]byte(data), folder); err != nil {
  176. log.Error("json.Unmarshal(%s) error(%v)", data, err)
  177. return
  178. }
  179. res[folder.ID] = folder
  180. }
  181. return
  182. }
  183. // RelationCntCache return the folder all relation count from redis.
  184. func (d *Dao) RelationCntCache(c context.Context, mid, fid int64) (cnt int, err error) {
  185. key := relationKey(mid, fid)
  186. conn := d.redis.Get(c)
  187. defer conn.Close()
  188. if cnt, err = redis.Int(conn.Do("ZCARD", key)); err != nil {
  189. if err == redis.ErrNil {
  190. return model.CacheNotFound, nil
  191. }
  192. log.Error("conn.Do(ZCARD %s) error(%v)", key, err)
  193. }
  194. return
  195. }
  196. // MaxScore get the max score from sorted set
  197. func (d *Dao) MaxScore(c context.Context, m *favmdl.Favorite) (score int64, err error) {
  198. key := allRelationKey(m.Mid, m.Fid)
  199. conn := d.redis.Get(c)
  200. defer conn.Close()
  201. values, err := redis.Values(conn.Do("ZREVRANGE", key, 0, 0, "WITHSCORES"))
  202. if err != nil {
  203. log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
  204. return
  205. }
  206. if len(values) != 2 {
  207. err = fmt.Errorf("redis zrange items(%v) length not 2", values)
  208. return
  209. }
  210. var id int64
  211. redis.Scan(values, &id, &score)
  212. return
  213. }
  214. // AddAllRelationCache add a relation to redis.
  215. func (d *Dao) AddAllRelationCache(c context.Context, m *favmdl.Favorite) (err error) {
  216. key := allRelationKey(m.Mid, m.Fid)
  217. score, err := d.MaxScore(c, m)
  218. if err != nil {
  219. return
  220. }
  221. if score <= 0 {
  222. log.Error("dao.AddAllRelationCache invalid score(%d)!%+v", d, *m)
  223. return
  224. }
  225. seq := int64(score/1e10) + 1
  226. conn := d.redis.Get(c)
  227. defer conn.Close()
  228. if err = conn.Send("ZADD", key, seq*1e10+int64(m.MTime), m.Oid*100+int64(m.Type)); err != nil {
  229. log.Error("conn.Send(ZADD %s,%d) error(%v)", key, m.Oid, err)
  230. return
  231. }
  232. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  233. log.Error("conn.Send(EXPIRE) error(%v)", err)
  234. return
  235. }
  236. if err = conn.Flush(); err != nil {
  237. log.Error("conn.Flush() error(%v)", err)
  238. return
  239. }
  240. for i := 0; i < 2; i++ {
  241. if _, err = conn.Receive(); err != nil {
  242. log.Error("conn.Receive() error(%v)", err)
  243. return
  244. }
  245. }
  246. return
  247. }
  248. // AddRelationCache add a relation to redis.
  249. func (d *Dao) AddRelationCache(c context.Context, m *favmdl.Favorite) (err error) {
  250. key := relationKey(m.Mid, m.Fid)
  251. conn := d.redis.Get(c)
  252. defer conn.Close()
  253. if err = conn.Send("ZADD", key, m.MTime, m.Oid); err != nil {
  254. log.Error("conn.Send(ZADD %s,%d) error(%v)", key, m.Oid, err)
  255. return
  256. }
  257. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  258. log.Error("conn.Send(EXPIRE) error(%v)", err)
  259. return
  260. }
  261. if err = conn.Flush(); err != nil {
  262. log.Error("conn.Flush() error(%v)", err)
  263. return
  264. }
  265. for i := 0; i < 2; i++ {
  266. if _, err = conn.Receive(); err != nil {
  267. log.Error("conn.Receive() error(%v)", err)
  268. return
  269. }
  270. }
  271. return
  272. }
  273. // AddAllRelationsCache add a relation to redis.
  274. func (d *Dao) AddAllRelationsCache(c context.Context, mid, fid int64, fs []*favmdl.Favorite) (err error) {
  275. key := allRelationKey(mid, fid)
  276. conn := d.redis.Get(c)
  277. defer conn.Close()
  278. for _, fav := range fs {
  279. if err = conn.Send("ZADD", key, fav.Sequence*1e10+uint64(fav.MTime), fav.Oid*100+int64(fav.Type)); err != nil {
  280. log.Error("conn.Send(ZADD %s,%d) error(%v)", key, fav.Oid, err)
  281. return
  282. }
  283. }
  284. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  285. log.Error("conn.Send(EXPIRE) error(%v)", err)
  286. return
  287. }
  288. if err = conn.Flush(); err != nil {
  289. log.Error("conn.Flush() error(%v)", err)
  290. return
  291. }
  292. for i := 0; i < len(fs)+1; i++ {
  293. if _, err = conn.Receive(); err != nil {
  294. log.Error("conn.Receive() error(%v)", err)
  295. return
  296. }
  297. }
  298. return
  299. }
  300. // AddRelationsCache add a relation to redis.
  301. func (d *Dao) AddRelationsCache(c context.Context, tp int8, mid, fid int64, fs []*favmdl.Favorite) (err error) {
  302. key := relationKey(mid, fid)
  303. conn := d.redis.Get(c)
  304. defer conn.Close()
  305. for _, fav := range fs {
  306. if err = conn.Send("ZADD", key, fav.MTime, fav.Oid); err != nil {
  307. log.Error("conn.Send(ZADD %s,%d) error(%v)", key, fav.Oid, err)
  308. return
  309. }
  310. }
  311. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  312. log.Error("conn.Send(EXPIRE) error(%v)", err)
  313. return
  314. }
  315. if err = conn.Flush(); err != nil {
  316. log.Error("conn.Flush() error(%v)", err)
  317. return
  318. }
  319. for i := 0; i < len(fs)+1; i++ {
  320. if _, err = conn.Receive(); err != nil {
  321. log.Error("conn.Receive() error(%v)", err)
  322. return
  323. }
  324. }
  325. return
  326. }
  327. // DelRelationsCache delete the folder relation cache.
  328. func (d *Dao) DelRelationsCache(c context.Context, mid, fid int64) (err error) {
  329. key := relationKey(mid, fid)
  330. conn := d.redis.Get(c)
  331. defer conn.Close()
  332. if _, err = conn.Do("DEL", key); err != nil {
  333. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  334. }
  335. return
  336. }
  337. // DelAllRelationsCache delete the folder relation cache.
  338. func (d *Dao) DelAllRelationsCache(c context.Context, mid, fid int64) (err error) {
  339. key := allRelationKey(mid, fid)
  340. conn := d.redis.Get(c)
  341. defer conn.Close()
  342. if _, err = conn.Do("DEL", key); err != nil {
  343. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  344. }
  345. return
  346. }
  347. // DelRelationCache delete one relation cache.
  348. func (d *Dao) DelRelationCache(c context.Context, mid, fid, oid int64) (err error) {
  349. key := relationKey(mid, fid)
  350. conn := d.redis.Get(c)
  351. defer conn.Close()
  352. if err = conn.Send("ZREM", key, oid); err != nil {
  353. log.Error("conn.Send error(%v)", err)
  354. return
  355. }
  356. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  357. log.Error("conn.Send error(%v)", err)
  358. return
  359. }
  360. if err = conn.Flush(); err != nil {
  361. log.Error("conn.Flush error(%v)", err)
  362. return
  363. }
  364. for i := 0; i < 2; i++ {
  365. if _, err = conn.Receive(); err != nil {
  366. log.Error("conn.Receive() error(%v)", err)
  367. return
  368. }
  369. }
  370. return
  371. }
  372. // DelAllRelationCache delete one relation cache.
  373. func (d *Dao) DelAllRelationCache(c context.Context, mid, fid, oid int64, typ int8) (err error) {
  374. key := allRelationKey(mid, fid)
  375. conn := d.redis.Get(c)
  376. defer conn.Close()
  377. if err = conn.Send("ZREM", key, oid*100+int64(typ)); err != nil {
  378. log.Error("conn.Send error(%v)", err)
  379. return
  380. }
  381. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  382. log.Error("conn.Send error(%v)", err)
  383. return
  384. }
  385. if err = conn.Flush(); err != nil {
  386. log.Error("conn.Flush error(%v)", err)
  387. return
  388. }
  389. for i := 0; i < 2; i++ {
  390. if _, err = conn.Receive(); err != nil {
  391. log.Error("conn.Receive() error(%v)", err)
  392. return
  393. }
  394. }
  395. return
  396. }
  397. // DelOldRelationsCache delete the folder relation cache. TODO:del at 2018.06.08
  398. func (d *Dao) DelOldRelationsCache(c context.Context, typ int8, mid, fid int64) (err error) {
  399. key := oldRelationKey(typ, mid, fid)
  400. conn := d.redis.Get(c)
  401. defer conn.Close()
  402. if _, err = conn.Do("DEL", key); err != nil {
  403. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  404. }
  405. return
  406. }
  407. // ExpireRelationOids set expire for faved oids.
  408. func (d *Dao) ExpireRelationOids(c context.Context, tp int8, mid int64) (ok bool, err error) {
  409. key := relationOidsKey(tp, mid)
  410. var conn = d.redis.Get(c)
  411. defer conn.Close()
  412. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
  413. log.Error("conn.Do(EXPIRE, %s) error(%v)", key, err)
  414. }
  415. return
  416. }
  417. // AddRelationOidCache add favoured oid.
  418. func (d *Dao) AddRelationOidCache(c context.Context, tp int8, mid, oid int64) (err error) {
  419. var (
  420. key = relationOidsKey(tp, mid)
  421. conn = d.redis.Get(c)
  422. )
  423. defer conn.Close()
  424. if err = conn.Send("SADD", key, oid); err != nil {
  425. log.Error("conn.Send error(%v)", err)
  426. return
  427. }
  428. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  429. log.Error("conn.Send error(%v)", err)
  430. return
  431. }
  432. if err = conn.Flush(); err != nil {
  433. log.Error("conn.Flush error(%v)", err)
  434. return
  435. }
  436. for i := 0; i < 2; i++ {
  437. if _, err = conn.Receive(); err != nil {
  438. log.Error("conn.Receive() error(%v)", err)
  439. return
  440. }
  441. }
  442. return
  443. }
  444. // RemRelationOidCache del favoured oid.
  445. func (d *Dao) RemRelationOidCache(c context.Context, tp int8, mid, oid int64) (err error) {
  446. var (
  447. key = relationOidsKey(tp, mid)
  448. conn = d.redis.Get(c)
  449. )
  450. defer conn.Close()
  451. if _, err = conn.Do("SREM", key, oid); err != nil {
  452. log.Error("conn.Do(%s,%d) error(%v)", key, oid, err)
  453. }
  454. return
  455. }
  456. // SetRelationOidsCache set favoured oids .
  457. func (d *Dao) SetRelationOidsCache(c context.Context, tp int8, mid int64, oids []int64) (err error) {
  458. var (
  459. key = relationOidsKey(tp, mid)
  460. conn = d.redis.Get(c)
  461. )
  462. defer conn.Close()
  463. for _, oid := range oids {
  464. if err = conn.Send("SADD", key, oid); err != nil {
  465. log.Error("conn.Send error(%v)", err)
  466. return
  467. }
  468. }
  469. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  470. log.Error("conn.Send error(%v)", err)
  471. return
  472. }
  473. if err = conn.Flush(); err != nil {
  474. log.Error("conn.Flush error(%v)", err)
  475. return
  476. }
  477. for i := 0; i < len(oids)+1; i++ {
  478. if _, err = conn.Receive(); err != nil {
  479. log.Error("conn.Receive() error(%v)", err)
  480. return
  481. }
  482. }
  483. return
  484. }
  485. // SetCleanedCache .
  486. func (d *Dao) SetCleanedCache(c context.Context, typ int8, mid, fid, ftime, expire int64) (err error) {
  487. var (
  488. key = cleanedKey(typ, mid)
  489. conn = d.redis.Get(c)
  490. )
  491. defer conn.Close()
  492. if err = conn.Send("HSET", key, fid, ftime); err != nil {
  493. log.Error("conn.Send error(%v)", err)
  494. return
  495. }
  496. if err = conn.Send("EXPIRE", key, expire); err != nil {
  497. log.Error("conn.Send error(%v)", err)
  498. return
  499. }
  500. if err = conn.Flush(); err != nil {
  501. log.Error("conn.Flush error(%v)", err)
  502. return
  503. }
  504. for i := 0; i < 2; i++ {
  505. if _, err = conn.Receive(); err != nil {
  506. log.Error("conn.Receive() error(%v)", err)
  507. return
  508. }
  509. }
  510. return
  511. }
  512. // DelRelationOidsCache .
  513. func (d *Dao) DelRelationOidsCache(c context.Context, typ int8, mid int64) (err error) {
  514. key := relationOidsKey(typ, mid)
  515. conn := d.redis.Get(c)
  516. defer conn.Close()
  517. if _, err = conn.Do("DEL", key); err != nil {
  518. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  519. }
  520. return
  521. }