mc.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // CookieCache get cookie info from cache
  16. func (d *Dao) CookieCache(c context.Context, session string) (res *model.Cookie, err error) {
  17. key := ckKey(session)
  18. conn := d.authMC.Get(c)
  19. defer conn.Close()
  20. var item *memcache.Item
  21. if item, err = conn.Get(key); err != nil {
  22. if err == memcache.ErrNotFound {
  23. err = nil
  24. return
  25. }
  26. log.Error("conn.Get(%s) error(%v)", key, err)
  27. return
  28. }
  29. res = new(model.Cookie)
  30. if err = conn.Scan(item, res); err != nil {
  31. log.Error("conn.Scan(%v) error(%v)", string(item.Value), err)
  32. }
  33. return
  34. }
  35. // DelCookieCache del cache.
  36. func (d *Dao) DelCookieCache(c context.Context, session string) (err error) {
  37. conn := d.authMC.Get(c)
  38. defer conn.Close()
  39. if err = conn.Delete(ckKey(session)); err != nil {
  40. if err == memcache.ErrNotFound {
  41. err = nil
  42. return
  43. }
  44. log.Error("conn.Delete(%s) error(%v)", ckKey(session), err)
  45. }
  46. return
  47. }
  48. // TokenCache get token from cache
  49. func (d *Dao) TokenCache(c context.Context, sd string) (res *model.Token, err error) {
  50. key := akKey(sd)
  51. conn := d.authMC.Get(c)
  52. defer conn.Close()
  53. r, err := conn.Get(key)
  54. if err != nil {
  55. if err == memcache.ErrNotFound {
  56. err = nil
  57. return
  58. }
  59. log.Error("conn.Get(%s) error(%v)", key, err)
  60. return
  61. }
  62. res = new(model.Token)
  63. if err = conn.Scan(r, res); err != nil {
  64. log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
  65. }
  66. return
  67. }
  68. // DelTokenCache del cache.
  69. func (d *Dao) DelTokenCache(c context.Context, token string) (err error) {
  70. key := akKey(token)
  71. conn := d.authMC.Get(c)
  72. defer conn.Close()
  73. if err = conn.Delete(key); err != nil {
  74. if err == memcache.ErrNotFound {
  75. err = nil
  76. return
  77. }
  78. log.Error("conn.Delete(%s) error(%v)", key, err)
  79. }
  80. return
  81. }