redis.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. model "go-common/app/service/main/secure/model"
  9. "go-common/library/cache/redis"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _prefixMsg = "m_"
  14. _prefixUnNotify = "d_%d%d_%d"
  15. _prefixCount = "c_%d%d_%d"
  16. _prefixChangePWD = "cpwd_%d"
  17. _prefixDoublecheck = "dc_%d"
  18. _expire = 24 * 3600
  19. _expirePWD = 30 * 24 * 3600
  20. )
  21. func doubleCheckKey(mid int64) string {
  22. return fmt.Sprintf(_prefixDoublecheck, mid)
  23. }
  24. func changePWDKey(mid int64) string {
  25. return fmt.Sprintf(_prefixChangePWD, mid)
  26. }
  27. func msgKey(mid int64) string {
  28. return _prefixMsg + strconv.FormatInt(mid, 10)
  29. }
  30. func unnotifyKey(mid int64) string {
  31. t := time.Now()
  32. return fmt.Sprintf(_prefixUnNotify, t.Month(), t.Day(), mid)
  33. }
  34. func countKey(mid int64) string {
  35. t := time.Now()
  36. return fmt.Sprintf(_prefixCount, t.Month(), t.Day(), mid)
  37. }
  38. // AddExpectionMsg add user login expection msg.
  39. func (d *Dao) AddExpectionMsg(c context.Context, l *model.Log) (err error) {
  40. var (
  41. conn = d.redis.Get(c)
  42. bs []byte
  43. key = msgKey(l.Mid)
  44. )
  45. defer conn.Close()
  46. if bs, err = json.Marshal(l); err != nil {
  47. log.Error("json.Marshal(%v) err(%v)", l, err)
  48. return
  49. }
  50. if _, err = conn.Do("SETEX", key, d.expire, bs); err != nil {
  51. log.Error("conn.Set msg:%v err(%v)", l, err)
  52. }
  53. return
  54. }
  55. // ExpectionMsg get user expection msg.
  56. func (d *Dao) ExpectionMsg(c context.Context, mid int64) (msg *model.Log, err error) {
  57. var (
  58. conn = d.redis.Get(c)
  59. bs []byte
  60. )
  61. defer conn.Close()
  62. if bs, err = redis.Bytes(conn.Do("GET", msgKey(mid))); err != nil {
  63. if err == redis.ErrNil {
  64. err = nil
  65. return
  66. }
  67. log.Error("conn.GET(mid %d) ,err(%v)", mid, err)
  68. return
  69. }
  70. msg = &model.Log{}
  71. if err = json.Unmarshal(bs, msg); err != nil {
  72. log.Error("json.Unmarshal err(%v)", err)
  73. }
  74. return
  75. }
  76. // AddUnNotify user unnotiry uuid.
  77. func (d *Dao) AddUnNotify(c context.Context, mid int64, uuid string) (err error) {
  78. var (
  79. conn = d.redis.Get(c)
  80. key = unnotifyKey(mid)
  81. )
  82. defer conn.Close()
  83. if err = conn.Send("SADD", key, uuid); err != nil {
  84. log.Error("conn.SADD mid:%d err(%v)", mid, err)
  85. return
  86. }
  87. if err = conn.Send("EXPIRE", key, _expire); err != nil {
  88. log.Error("EXPIRE key :%d err %d", key, err)
  89. return
  90. }
  91. conn.Flush()
  92. for i := 0; i < 2; i++ {
  93. if _, err = conn.Receive(); err != nil {
  94. log.Error("conn.Recive err %v", err)
  95. return
  96. }
  97. }
  98. return
  99. }
  100. // DelUnNotify del user unnotify record.
  101. func (d *Dao) DelUnNotify(c context.Context, mid int64) (err error) {
  102. conn := d.redis.Get(c)
  103. if _, err = conn.Do("DEL", unnotifyKey(mid)); err != nil {
  104. log.Error("conn.DEL mid:%d err:%v", mid, err)
  105. }
  106. conn.Close()
  107. return
  108. }
  109. // UnNotify check if not send notify to user of uuid deveice.
  110. func (d *Dao) UnNotify(c context.Context, mid int64, uuid string) (b bool, err error) {
  111. conn := d.redis.Get(c)
  112. if b, err = redis.Bool(conn.Do("SISMEMBER", unnotifyKey(mid), uuid)); err != nil {
  113. if err == redis.ErrNil {
  114. err = nil
  115. }
  116. log.Error("conn.SISMEMBER (mid:%d) ERR(%v)", mid, err)
  117. }
  118. conn.Close()
  119. return
  120. }
  121. // Count get user close notify count.
  122. func (d *Dao) Count(c context.Context, mid int64, uuid string) (count int64, err error) {
  123. conn := d.redis.Get(c)
  124. if count, err = redis.Int64(conn.Do("HGET", countKey(mid), uuid)); err != nil {
  125. if err == redis.ErrNil {
  126. err = nil
  127. }
  128. log.Error("conn.GET mid:%d err(%v)", mid, err)
  129. }
  130. conn.Close()
  131. return
  132. }
  133. // AddCount add user unnotify count daily.
  134. func (d *Dao) AddCount(c context.Context, mid int64, uuid string) (err error) {
  135. conn := d.redis.Get(c)
  136. defer conn.Close()
  137. if err = conn.Send("HINCRBY", countKey(mid), uuid, 1); err != nil {
  138. log.Error("conn.INCR mid:%d err:%v", mid, err)
  139. return
  140. }
  141. if err = conn.Send("EXPIRE", countKey(mid), _expire); err != nil {
  142. log.Error("conn.EXPIRE mid:%d err:%v", mid, err)
  143. return
  144. }
  145. conn.Flush()
  146. for i := 0; i < 2; i++ {
  147. if _, err1 := conn.Receive(); err1 != nil {
  148. log.Error("conn.Receive err(%v)", err1)
  149. return
  150. }
  151. }
  152. return
  153. }
  154. // AddChangePWDRecord set user change passwd record to cache.
  155. func (d *Dao) AddChangePWDRecord(c context.Context, mid int64) (err error) {
  156. conn := d.redis.Get(c)
  157. if _, err = conn.Do("SETEX", changePWDKey(mid), _expirePWD, 1); err != nil {
  158. log.Error("d.ChangePWDRecord(mid %d) err(%v)", mid, err)
  159. }
  160. conn.Close()
  161. return
  162. }
  163. // ChangePWDRecord check if user had change pwd recently one month.
  164. func (d *Dao) ChangePWDRecord(c context.Context, mid int64) (b bool, err error) {
  165. conn := d.redis.Get(c)
  166. defer conn.Close()
  167. if b, err = redis.Bool(conn.Do("GET", changePWDKey(mid))); err != nil {
  168. if err == redis.ErrNil {
  169. err = nil
  170. return
  171. }
  172. log.Error("d.ChangePWDRecord err(%v)", err)
  173. }
  174. return
  175. }
  176. // DelCount del count
  177. // for testing clear data.
  178. func (d *Dao) DelCount(c context.Context, mid int64) (err error) {
  179. conn := d.redis.Get(c)
  180. defer conn.Close()
  181. _, err = conn.Do("DEL", countKey(mid))
  182. return
  183. }
  184. // AddDCheckCache add double check cache.
  185. func (d *Dao) AddDCheckCache(c context.Context, mid int64) (err error) {
  186. conn := d.redis.Get(c)
  187. if _, err = conn.Do("SETEX", doubleCheckKey(mid), d.doubleCheckExpire, 1); err != nil {
  188. log.Error("d.AddDCheckCache(mid %d) err(%v)", mid, err)
  189. }
  190. conn.Close()
  191. return
  192. }
  193. // DCheckCache check if user had notify by double check.
  194. func (d *Dao) DCheckCache(c context.Context, mid int64) (b bool, err error) {
  195. conn := d.redis.Get(c)
  196. defer conn.Close()
  197. if b, err = redis.Bool(conn.Do("GET", doubleCheckKey(mid))); err != nil {
  198. if err == redis.ErrNil {
  199. err = nil
  200. return
  201. }
  202. log.Error("d.DCheckCache err(%v)", err)
  203. }
  204. return
  205. }