mc.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/passport-auth/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. func ckKey(session string) string {
  10. return fmt.Sprintf("ck_%s", session)
  11. }
  12. func akKey(token string) string {
  13. return fmt.Sprintf("ak_%s", token)
  14. }
  15. func rkKey(refresh string) string {
  16. return fmt.Sprintf("rk_%s", refresh)
  17. }
  18. // SetCookieCache set cookie info to cache
  19. func (d *Dao) SetCookieCache(c context.Context, session string, res *model.Cookie) (err error) {
  20. key := ckKey(session)
  21. conn := d.mc.Get(c)
  22. defer conn.Close()
  23. if res.Expires < 0 {
  24. log.Error("auth expire error(expires:%d)", res.Expires)
  25. return
  26. }
  27. item := &memcache.Item{Key: key, Object: res, Flags: memcache.FlagProtobuf, Expiration: int32(res.Expires)}
  28. if err = conn.Set(item); err != nil {
  29. log.Error("auth set error(%s,%d,%v)", key, res.Expires, err)
  30. }
  31. return
  32. }
  33. // CookieCache get cookie info from cache
  34. func (d *Dao) CookieCache(c context.Context, session string) (res *model.Cookie, err error) {
  35. key := ckKey(session)
  36. conn := d.mc.Get(c)
  37. defer conn.Close()
  38. var item *memcache.Item
  39. if item, err = conn.Get(key); err != nil {
  40. if err == memcache.ErrNotFound {
  41. err = nil
  42. return
  43. }
  44. log.Error("conn.Get(%s) error(%v)", key, err)
  45. return
  46. }
  47. res = new(model.Cookie)
  48. if err = conn.Scan(item, res); err != nil {
  49. log.Error("conn.Scan(%v) error(%v)", string(item.Value), err)
  50. }
  51. return
  52. }
  53. // DelCookieCache del cache.
  54. func (d *Dao) DelCookieCache(c context.Context, session string) (err error) {
  55. conn := d.mc.Get(c)
  56. defer conn.Close()
  57. if err = conn.Delete(ckKey(session)); err != nil {
  58. if err == memcache.ErrNotFound {
  59. err = nil
  60. return
  61. }
  62. log.Error("conn.Delete(%s) error(%v)", ckKey(session), err)
  63. }
  64. return
  65. }
  66. // SetTokenCache set token to cache
  67. func (d *Dao) SetTokenCache(c context.Context, k string, res *model.Token) (err error) {
  68. key := akKey(k)
  69. conn := d.mc.Get(c)
  70. defer conn.Close()
  71. if res.Expires < 0 {
  72. log.Error("auth expire error(expires:%d)", res.Expires)
  73. return
  74. }
  75. if err = conn.Set(&memcache.Item{
  76. Key: key,
  77. Object: res,
  78. Flags: memcache.FlagProtobuf,
  79. Expiration: int32(res.Expires),
  80. }); err != nil {
  81. log.Error("set token cache error(%s,%d,%v)", key, res.Expires, err)
  82. }
  83. return
  84. }
  85. // TokenCache get token from cache
  86. func (d *Dao) TokenCache(c context.Context, sd string) (res *model.Token, err error) {
  87. key := akKey(sd)
  88. conn := d.mc.Get(c)
  89. defer conn.Close()
  90. r, err := conn.Get(key)
  91. if err != nil {
  92. if err == memcache.ErrNotFound {
  93. err = nil
  94. return
  95. }
  96. log.Error("conn.Get(%s) error(%v)", key, err)
  97. return
  98. }
  99. res = new(model.Token)
  100. if err = conn.Scan(r, res); err != nil {
  101. log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
  102. }
  103. return
  104. }
  105. // DelTokenCache del cache.
  106. func (d *Dao) DelTokenCache(c context.Context, token string) (err error) {
  107. key := akKey(token)
  108. conn := d.mc.Get(c)
  109. defer conn.Close()
  110. if err = conn.Delete(key); err != nil {
  111. if err == memcache.ErrNotFound {
  112. err = nil
  113. return
  114. }
  115. log.Error("conn.Delete(%s) error(%v)", key, err)
  116. }
  117. return
  118. }
  119. // SetRefreshCache set refresh token to cache .
  120. func (d *Dao) SetRefreshCache(c context.Context, refresh *model.Refresh) (err error) {
  121. key := rkKey(refresh.Refresh)
  122. conn := d.mc.Get(c)
  123. defer conn.Close()
  124. if refresh.Expires < 0 {
  125. log.Error("auth expire error(expires:%d)", refresh.Expires)
  126. return
  127. }
  128. if err := conn.Set(&memcache.Item{
  129. Key: key,
  130. Object: refresh,
  131. Flags: memcache.FlagProtobuf,
  132. Expiration: int32(refresh.Expires),
  133. }); err != nil {
  134. log.Error("auth set error(%s,%d,%v)", key, refresh.Expires, err)
  135. }
  136. return
  137. }
  138. // RefreshCache get refresh token from cache
  139. func (d *Dao) RefreshCache(c context.Context, refresh string) (res *model.Refresh, err error) {
  140. key := rkKey(refresh)
  141. conn := d.mc.Get(c)
  142. defer conn.Close()
  143. r, err := conn.Get(key)
  144. if err != nil {
  145. if err == memcache.ErrNotFound {
  146. err = nil
  147. return
  148. }
  149. log.Error("conn.Get(%s) error(%v)", key, err)
  150. return
  151. }
  152. res = new(model.Refresh)
  153. if err = conn.Scan(r, res); err != nil {
  154. log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
  155. }
  156. return
  157. }
  158. // DelRefreshCache del refresh token from cache
  159. func (d *Dao) DelRefreshCache(c context.Context, refresh string) (err error) {
  160. key := akKey(refresh)
  161. conn := d.mc.Get(c)
  162. defer conn.Close()
  163. if err = conn.Delete(key); err != nil {
  164. if err == memcache.ErrNotFound {
  165. err = nil
  166. return
  167. }
  168. log.Error("conn.Delete(%s) error(%v)", key, err)
  169. }
  170. return
  171. }
  172. // pingMC ping memcache.
  173. func (d *Dao) pingMC(c context.Context) (err error) {
  174. conn := d.mc.Get(c)
  175. defer conn.Close()
  176. if err = conn.Set(&memcache.Item{
  177. Key: "ping",
  178. Value: []byte{1},
  179. Expiration: d.mcExpire,
  180. }); err != nil {
  181. log.Error("conn.Set(ping, 1) error(%v)", err)
  182. }
  183. return
  184. }