redis.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/service/main/favorite/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. )
  11. const (
  12. _folderKey = "fi_%d_%d" // sortedset fi_type_mid value:fid,score:ctime
  13. _relationKey = "r_%d_%d" // sortedset r_mid_fid(mtime, oid)
  14. _allRelationKey = "ar_%d_%d" // sortedset ar_mid_fid(mtime, oid)
  15. _relationOidsKey = "ro_%d_%d" // set ro_type_mid value:oids
  16. _cleanedKey = "rc_%d_%d" // hash key:rc_type_mid field:fid value:timestamp
  17. // key fb_mid/100000 offset => mid%100000
  18. // bit value 1 mean unfaved; bit value 0 mean faved
  19. _favedBit = "fb_%d_%d"
  20. _bucket = 100000
  21. )
  22. // isFavedKey return user's fav flag key
  23. func favedBitKey(tp int8, mid int64) string {
  24. return fmt.Sprintf(_favedBit, tp, mid/_bucket)
  25. }
  26. // folderKey return a user folder key.
  27. func folderKey(tp int8, mid int64) string {
  28. return fmt.Sprintf(_folderKey, tp, mid)
  29. }
  30. // relationKey return folder relation key.
  31. func relationKey(mid, fid int64) string {
  32. return fmt.Sprintf(_relationKey, mid, fid)
  33. }
  34. // relationKey return folder relation key.
  35. func allRelationKey(mid, fid int64) string {
  36. return fmt.Sprintf(_allRelationKey, mid, fid)
  37. }
  38. // relationOidsKey return a user oids key.
  39. func relationOidsKey(tp int8, mid int64) string {
  40. return fmt.Sprintf(_relationOidsKey, tp, mid)
  41. }
  42. // isCleanedKey return user whether cleaned key.
  43. func isCleanedKey(tp int8, mid int64) string {
  44. return fmt.Sprintf(_cleanedKey, tp, mid)
  45. }
  46. // pingRedis check redis connection
  47. func (d *Dao) pingRedis(c context.Context) (err error) {
  48. conn := d.redis.Get(c)
  49. _, err = conn.Do("SET", "PING", "PONG")
  50. conn.Close()
  51. return
  52. }
  53. // ExpireRelations expire folder relations cache.
  54. func (d *Dao) ExpireRelations(c context.Context, mid, fid int64) (ok bool, err error) {
  55. key := relationKey(mid, fid)
  56. conn := d.redis.Get(c)
  57. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
  58. log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
  59. }
  60. conn.Close()
  61. return
  62. }
  63. // ExpireAllRelations expire folder relations cache.
  64. func (d *Dao) ExpireAllRelations(c context.Context, mid, fid int64) (ok bool, err error) {
  65. key := allRelationKey(mid, fid)
  66. conn := d.redis.Get(c)
  67. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
  68. log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
  69. }
  70. conn.Close()
  71. return
  72. }
  73. // ExpireFolder expire folder cache.
  74. func (d *Dao) ExpireFolder(c context.Context, tp int8, mid int64) (ok bool, err error) {
  75. key := folderKey(tp, mid)
  76. conn := d.redis.Get(c)
  77. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
  78. log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
  79. }
  80. conn.Close()
  81. return
  82. }
  83. // RemFidsRedis del user's fids in redis
  84. func (d *Dao) RemFidsRedis(c context.Context, typ int8, mid int64, fs ...*model.Folder) (err error) {
  85. var (
  86. key = folderKey(typ, mid)
  87. conn = d.redis.Get(c)
  88. )
  89. defer conn.Close()
  90. args := []interface{}{key}
  91. for _, f := range fs {
  92. args = append(args, f.ID)
  93. }
  94. if err = conn.Send("ZREM", args...); err != nil {
  95. log.Error("conn.Send(ZREM %s,%v) error(%v)", key, args, err)
  96. return
  97. }
  98. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  99. log.Error("conn.Send(EXPIRE) error(%v)", err)
  100. return
  101. }
  102. if err = conn.Flush(); err != nil {
  103. log.Error("conn.Flush() error(%v)", err)
  104. return
  105. }
  106. for i := 0; i < len(fs)+1; i++ {
  107. if _, err = conn.Receive(); err != nil {
  108. log.Error("conn.Receive() error(%v)", err)
  109. return
  110. }
  111. }
  112. return
  113. }
  114. // AddFidsRedis set user's fids to redis
  115. func (d *Dao) AddFidsRedis(c context.Context, typ int8, mid int64, fs ...*model.Folder) (err error) {
  116. var (
  117. key = folderKey(typ, mid)
  118. conn = d.redis.Get(c)
  119. )
  120. defer conn.Close()
  121. for _, f := range fs {
  122. if err = conn.Send("ZADD", key, f.CTime, f.ID); err != nil {
  123. log.Error("conn.Send(ZADD %s,%s,%d) error(%v)", key, f.CTime, f.ID, err)
  124. return
  125. }
  126. }
  127. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  128. log.Error("conn.Send(EXPIRE) error(%v)", err)
  129. return
  130. }
  131. if err = conn.Flush(); err != nil {
  132. log.Error("conn.Flush() error(%v)", err)
  133. return
  134. }
  135. for i := 0; i < len(fs)+1; i++ {
  136. if _, err = conn.Receive(); err != nil {
  137. log.Error("conn.Receive() error(%v)", err)
  138. return
  139. }
  140. }
  141. return
  142. }
  143. // FidsRedis return the user's all fids from redis.
  144. func (d *Dao) FidsRedis(c context.Context, tp int8, mid int64) (fids []int64, err error) {
  145. var (
  146. key = folderKey(tp, mid)
  147. conn = d.redis.Get(c)
  148. )
  149. defer conn.Close()
  150. if fids, err = redis.Int64s(conn.Do("ZRANGE", key, 0, -1)); err != nil {
  151. log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
  152. return
  153. }
  154. return
  155. }
  156. // DelFidsRedis delete user's all fids from redis.
  157. func (d *Dao) DelFidsRedis(c context.Context, typ int8, mid int64) (err error) {
  158. var (
  159. key = folderKey(typ, mid)
  160. conn = d.redis.Get(c)
  161. )
  162. defer conn.Close()
  163. if _, err = conn.Do("DEL", key); err != nil {
  164. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  165. }
  166. return
  167. }
  168. // AddFoldersCache add the user all folders to redis.
  169. func (d *Dao) AddFoldersCache(c context.Context, tp int8, mid int64, folders []*model.Folder) (err error) {
  170. var (
  171. folder *model.Folder
  172. value []byte
  173. key = folderKey(tp, mid)
  174. conn = d.redis.Get(c)
  175. )
  176. defer conn.Close()
  177. for _, folder = range folders {
  178. if value, err = json.Marshal(folder); err != nil {
  179. return
  180. }
  181. if err = conn.Send("HSET", key, folder.ID, value); err != nil {
  182. log.Error("conn.Send(HSET %s,%d,%s) error(%v)", key, folder.ID, value)
  183. return
  184. }
  185. }
  186. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  187. log.Error("conn.Send(EXPIRE) error(%v)", err)
  188. return
  189. }
  190. if err = conn.Flush(); err != nil {
  191. log.Error("conn.Flush error(%v)", err)
  192. return
  193. }
  194. for i := 0; i < len(folders)+1; i++ {
  195. if _, err = conn.Receive(); err != nil {
  196. log.Error("conn.Receive error(%v)", err)
  197. return
  198. }
  199. }
  200. return
  201. }
  202. // FolderRelationsCache return the folder all relations from redis.
  203. func (d *Dao) FolderRelationsCache(c context.Context, typ int8, mid, fid int64, start, end int) (res []*model.Favorite, err error) {
  204. conn := d.redis.Get(c)
  205. key := relationKey(mid, fid)
  206. defer conn.Close()
  207. values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end, "WITHSCORES"))
  208. if err != nil {
  209. log.Error("conn.Do(ZREVRANGE,%s,%d,%d) error(%v)", key, start, end, err)
  210. return
  211. }
  212. if len(values) == 0 {
  213. return
  214. }
  215. var oid, t int64
  216. for len(values) > 0 {
  217. if values, err = redis.Scan(values, &oid, &t); err != nil {
  218. log.Error("redis.Scan() error(%v)", err)
  219. return
  220. }
  221. res = append(res, &model.Favorite{Oid: oid, Mid: mid, Fid: fid, Type: typ, MTime: xtime.Time(t)})
  222. }
  223. return
  224. }
  225. // CntRelationsCache return the folder all relation count from redis.
  226. func (d *Dao) CntRelationsCache(c context.Context, mid, fid int64) (cnt int, err error) {
  227. var conn = d.redis.Get(c)
  228. key := relationKey(mid, fid)
  229. defer conn.Close()
  230. if cnt, err = redis.Int(conn.Do("ZCARD", key)); err != nil {
  231. if err == redis.ErrNil {
  232. err = nil
  233. }
  234. log.Error("conn.Do(ZCARD %s) error(%v)", key, err)
  235. return
  236. }
  237. return
  238. }
  239. // CntAllRelationsCache return the folder all relation count from redis.
  240. func (d *Dao) CntAllRelationsCache(c context.Context, mid, fid int64) (cnt int, err error) {
  241. var conn = d.redis.Get(c)
  242. key := allRelationKey(mid, fid)
  243. defer conn.Close()
  244. if cnt, err = redis.Int(conn.Do("ZCARD", key)); err != nil {
  245. if err == redis.ErrNil {
  246. err = nil
  247. }
  248. log.Error("conn.Do(ZCARD %s) error(%v)", key, err)
  249. return
  250. }
  251. return
  252. }
  253. // FolderRelationsCache return the folder all relations from redis.
  254. func (d *Dao) FolderAllRelationsCache(c context.Context, typ int8, mid, fid int64, start, end int) (res []*model.Favorite, err error) {
  255. conn := d.redis.Get(c)
  256. key := allRelationKey(mid, fid)
  257. defer conn.Close()
  258. values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end, "WITHSCORES"))
  259. if err != nil {
  260. log.Error("conn.Do(ZREVRANGE,%s,%d,%d) error(%v)", key, start, end, err)
  261. return
  262. }
  263. if len(values) == 0 {
  264. return
  265. }
  266. var oid, score int64
  267. for len(values) > 0 {
  268. if values, err = redis.Scan(values, &oid, &score); err != nil {
  269. log.Error("redis.Scan() error(%v)", err)
  270. return
  271. }
  272. res = append(res, &model.Favorite{Oid: oid / 100, Mid: mid, Fid: fid, Type: int8(oid % 100), MTime: xtime.Time(score % 1e10)})
  273. }
  274. return
  275. }
  276. // MultiExpireRelations expire folders's relations cache.
  277. func (d *Dao) MultiExpireAllRelations(c context.Context, mid int64, fids []int64) (map[int64]bool, error) {
  278. okMap := make(map[int64]bool, len(fids))
  279. conn := d.redis.Get(c)
  280. defer conn.Close()
  281. for _, fid := range fids {
  282. key := allRelationKey(mid, fid)
  283. if err := conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  284. log.Error("redis.send(EXPIRE %s) error(%v)", key, err)
  285. return nil, err
  286. }
  287. }
  288. if err := conn.Flush(); err != nil {
  289. log.Error("conn.Flush error(%v)", err)
  290. return nil, err
  291. }
  292. for _, fid := range fids {
  293. ok, err := redis.Bool(conn.Receive())
  294. if err != nil {
  295. log.Error("conn.Receive(%d) error(%v)", fid, err)
  296. return nil, err
  297. }
  298. okMap[fid] = ok
  299. }
  300. return okMap, nil
  301. }
  302. // MultiExpireRelations expire folders's relations cache.
  303. func (d *Dao) MultiExpireRelations(c context.Context, mid int64, fids []int64) (map[int64]bool, error) {
  304. okMap := make(map[int64]bool, len(fids))
  305. conn := d.redis.Get(c)
  306. defer conn.Close()
  307. for _, fid := range fids {
  308. key := relationKey(mid, fid)
  309. if err := conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  310. log.Error("redis.send(EXPIRE %s) error(%v)", key, err)
  311. return nil, err
  312. }
  313. }
  314. if err := conn.Flush(); err != nil {
  315. log.Error("conn.Flush error(%v)", err)
  316. return nil, err
  317. }
  318. for _, fid := range fids {
  319. ok, err := redis.Bool(conn.Receive())
  320. if err != nil {
  321. log.Error("conn.Receive(%d) error(%v)", fid, err)
  322. return nil, err
  323. }
  324. okMap[fid] = ok
  325. }
  326. return okMap, nil
  327. }
  328. // ExpireRelationOids set expire for faved oids.
  329. func (d *Dao) ExpireRelationOids(c context.Context, tp int8, mid int64) (ok bool, err error) {
  330. key := relationOidsKey(tp, mid)
  331. var conn = d.redis.Get(c)
  332. defer conn.Close()
  333. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
  334. log.Error("conn.Do(EXPIRE, %s) error(%v)", key, err)
  335. }
  336. return
  337. }
  338. // AddRelationOidCache add favoured oid.
  339. func (d *Dao) AddRelationOidCache(c context.Context, tp int8, mid, oid int64) (err error) {
  340. var (
  341. key = relationOidsKey(tp, mid)
  342. conn = d.redis.Get(c)
  343. )
  344. defer conn.Close()
  345. if err = conn.Send("SADD", key, oid); err != nil {
  346. log.Error("conn.Send error(%v)", err)
  347. return
  348. }
  349. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  350. log.Error("conn.Send error(%v)", err)
  351. return
  352. }
  353. if err = conn.Flush(); err != nil {
  354. log.Error("conn.Flush error(%v)", err)
  355. return
  356. }
  357. for i := 0; i < 2; i++ {
  358. if _, err = conn.Receive(); err != nil {
  359. log.Error("conn.Receive() error(%v)", err)
  360. return
  361. }
  362. }
  363. return
  364. }
  365. // RemRelationOidCache del faved oid.
  366. func (d *Dao) RemRelationOidCache(c context.Context, tp int8, mid, oid int64) (err error) {
  367. var (
  368. key = relationOidsKey(tp, mid)
  369. conn = d.redis.Get(c)
  370. )
  371. defer conn.Close()
  372. if err = conn.Send("SREM", key, oid); err != nil {
  373. log.Error("conn.Send error(%v)", err)
  374. return
  375. }
  376. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  377. log.Error("conn.Send error(%v)", err)
  378. return
  379. }
  380. if err = conn.Flush(); err != nil {
  381. log.Error("conn.Flush error(%v)", err)
  382. return
  383. }
  384. for i := 0; i < 2; i++ {
  385. if _, err = conn.Receive(); err != nil {
  386. log.Error("conn.Receive() error(%v)", err)
  387. return
  388. }
  389. }
  390. return
  391. }
  392. // IsFavedCache return true or false to judge object whether faved by user.
  393. func (d *Dao) IsFavedCache(c context.Context, tp int8, mid int64, oid int64) (isFaved bool, err error) {
  394. var (
  395. key = relationOidsKey(tp, mid)
  396. conn = d.redis.Get(c)
  397. )
  398. defer conn.Close()
  399. if isFaved, err = redis.Bool(conn.Do("SISMEMBER", key, oid)); err != nil {
  400. if err == redis.ErrNil {
  401. err = nil
  402. } else {
  403. log.Error("HGET %v %v error(%v)", key, oid, err)
  404. }
  405. }
  406. return
  407. }
  408. // IsFavedsCache return true or false to judge object whether faved by user.
  409. func (d *Dao) IsFavedsCache(c context.Context, tp int8, mid int64, oids []int64) (favoreds map[int64]bool, err error) {
  410. var (
  411. key = relationOidsKey(tp, mid)
  412. conn = d.redis.Get(c)
  413. )
  414. defer conn.Close()
  415. for _, oid := range oids {
  416. if err = conn.Send("SISMEMBER", key, oid); err != nil {
  417. log.Error("conn.Send(SISMEMBER %s,%d) error(%v)", key, oid, err)
  418. return
  419. }
  420. }
  421. if err = conn.Flush(); err != nil {
  422. log.Error("conn.Flush() error(%v)", err)
  423. return
  424. }
  425. favoreds = make(map[int64]bool, len(oids))
  426. for _, oid := range oids {
  427. faved, err := redis.Bool(conn.Receive())
  428. if err != nil {
  429. log.Error("conn.Receive() error(%v)", err)
  430. continue
  431. }
  432. favoreds[oid] = faved
  433. }
  434. return
  435. }
  436. // SetFavedBit set unfaved user bit to 0
  437. func (d *Dao) SetFavedBit(c context.Context, tp int8, mid int64) (err error) {
  438. key := favedBitKey(tp, mid)
  439. offset := mid % _bucket
  440. conn := d.redis.Get(c)
  441. defer conn.Close()
  442. if _, err = conn.Do("SETBIT", key, offset, 0); err != nil {
  443. log.Error("conn.DO(SETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
  444. }
  445. return
  446. }
  447. // SetUnFavedBit set unfaved user bit to 1
  448. func (d *Dao) SetUnFavedBit(c context.Context, tp int8, mid int64) (err error) {
  449. key := favedBitKey(tp, mid)
  450. offset := mid % _bucket
  451. conn := d.redis.Get(c)
  452. defer conn.Close()
  453. if _, err = conn.Do("SETBIT", key, offset, 1); err != nil {
  454. log.Error("conn.DO(SETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
  455. }
  456. return
  457. }
  458. // FavedBit check if user had fav video by bit value
  459. func (d *Dao) FavedBit(c context.Context, tp int8, mid int64) (unfaved bool, err error) {
  460. key := favedBitKey(tp, mid)
  461. offset := mid % _bucket
  462. conn := d.redis.Get(c)
  463. defer conn.Close()
  464. if unfaved, err = redis.Bool(conn.Do("GETBIT", key, offset)); err != nil {
  465. log.Error("conn.DO(GETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
  466. }
  467. return
  468. }
  469. // DelRelationsCache delete the folder relation cache.
  470. func (d *Dao) DelAllRelationsCache(c context.Context, mid, fid int64) (err error) {
  471. key := allRelationKey(mid, fid)
  472. conn := d.redis.Get(c)
  473. defer conn.Close()
  474. if _, err = conn.Do("DEL", key); err != nil {
  475. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  476. }
  477. return
  478. }
  479. // DelRelationsCache delete the folder relation cache.
  480. func (d *Dao) DelRelationsCache(c context.Context, mid, fid int64) (err error) {
  481. key := relationKey(mid, fid)
  482. conn := d.redis.Get(c)
  483. defer conn.Close()
  484. if _, err = conn.Do("DEL", key); err != nil {
  485. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  486. }
  487. return
  488. }
  489. // AddRelationCache add a relation to redis.
  490. func (d *Dao) AddRelationCache(c context.Context, m *model.Favorite) (err error) {
  491. key := relationKey(m.Mid, m.Fid)
  492. conn := d.redis.Get(c)
  493. defer conn.Close()
  494. if err = conn.Send("ZADD", key, m.MTime, m.Oid); err != nil {
  495. log.Error("conn.Send(ZADD %s,%d) error(%v)", key, m.Oid, err)
  496. return
  497. }
  498. if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
  499. log.Error("conn.Send(EXPIRE) error(%v)", err)
  500. return
  501. }
  502. if err = conn.Flush(); err != nil {
  503. log.Error("conn.Flush() error(%v)", err)
  504. return
  505. }
  506. for i := 0; i < 2; i++ {
  507. if _, err = conn.Receive(); err != nil {
  508. log.Error("conn.Receive() error(%v)", err)
  509. return
  510. }
  511. }
  512. return
  513. }
  514. // RecentOidsCache return three recent oids of all user's folders from redis.
  515. func (d *Dao) RecentOidsCache(c context.Context, typ int8, mid int64, fids []int64) (rctFidsMap map[int64][]int64, missFids []int64, err error) {
  516. conn := d.redis.Get(c)
  517. defer conn.Close()
  518. for _, fid := range fids {
  519. key := relationKey(mid, fid)
  520. if err = conn.Send("ZREVRANGE", key, 0, 2); err != nil {
  521. log.Error("conn.Do(ZREVRANGE, %s,%d) error(%v)", key, fid, err)
  522. return
  523. }
  524. }
  525. if err = conn.Flush(); err != nil {
  526. log.Error("conn.Flush error(%v)", err)
  527. return
  528. }
  529. rctFidsMap = make(map[int64][]int64, len(fids))
  530. for _, fid := range fids {
  531. oids, err := redis.Int64s(conn.Receive())
  532. if err != nil {
  533. log.Error("redis.Strings()err(%v)", err)
  534. return nil, fids, err
  535. }
  536. if len(oids) == 0 {
  537. missFids = append(missFids, fid)
  538. }
  539. rctFidsMap[fid] = oids
  540. }
  541. return
  542. }
  543. // BatchOidsRedis return the user's 1000 oids from redis.
  544. func (d *Dao) BatchOidsRedis(c context.Context, tp int8, mid int64, limit int) (oids []int64, err error) {
  545. var (
  546. key = relationOidsKey(tp, mid)
  547. conn = d.redis.Get(c)
  548. )
  549. defer conn.Close()
  550. if oids, err = redis.Int64s(conn.Do("SRANDMEMBER", key, limit)); err != nil {
  551. log.Error("conn.Do(SRANDMEMBER, %s) error(%v)", key, err)
  552. return
  553. }
  554. return
  555. }
  556. // IsCleaned check if user had do clean action
  557. func (d *Dao) IsCleaned(c context.Context, typ int8, mid, fid int64) (cleanedTime int64, err error) {
  558. var (
  559. key = isCleanedKey(typ, mid)
  560. conn = d.redis.Get(c)
  561. )
  562. defer conn.Close()
  563. if cleanedTime, err = redis.Int64(conn.Do("HGET", key, fid)); err != nil {
  564. if err == redis.ErrNil {
  565. err = nil
  566. cleanedTime = 0
  567. } else {
  568. log.Error("conn.Do(HGET, %v, %v) error(%v)", key, fid, err)
  569. }
  570. }
  571. return
  572. }
  573. // SetCleanedCache set cleand flag.
  574. func (d *Dao) SetCleanedCache(c context.Context, typ int8, mid, fid, ftime, expire int64) (err error) {
  575. var (
  576. key = isCleanedKey(typ, mid)
  577. conn = d.redis.Get(c)
  578. )
  579. defer conn.Close()
  580. if err = conn.Send("HSET", key, fid, ftime); err != nil {
  581. log.Error("conn.Send error(%v)", err)
  582. }
  583. if err = conn.Send("EXPIRE", key, expire); err != nil {
  584. log.Error("conn.Send error(%v)", err)
  585. return
  586. }
  587. if err = conn.Flush(); err != nil {
  588. log.Error("conn.Flush error(%v)", err)
  589. return
  590. }
  591. for i := 0; i < 2; i++ {
  592. if _, err = conn.Receive(); err != nil {
  593. log.Error("conn.Receive error(%v)", err)
  594. return
  595. }
  596. }
  597. return
  598. }
  599. // DelRelationOidsCache .
  600. func (d *Dao) DelRelationOidsCache(c context.Context, typ int8, mid int64) (err error) {
  601. key := relationOidsKey(typ, mid)
  602. conn := d.redis.Get(c)
  603. defer conn.Close()
  604. if _, err = conn.Do("DEL", key); err != nil {
  605. log.Error("conn.Do(DEL %s) error(%v)", key, err)
  606. }
  607. return
  608. }