share.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/bbq/app-bbq/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. )
  9. var (
  10. _selectUserShareToken = "select token from user_share_token where mid = ?;"
  11. _insertUserShareToken = "insert into user_share_token (mid, token) values (?, ?);"
  12. )
  13. // GetUserShareToken .
  14. func (d *Dao) GetUserShareToken(ctx context.Context, mid int64) string {
  15. conn := d.redis.Get(ctx)
  16. defer conn.Close()
  17. raw, err := redis.Bytes(conn.Do("GET", fmt.Sprintf(model.CacheKeyUserShareToken, mid)))
  18. if err == redis.ErrNil || raw == nil {
  19. rows, err := d.db.Query(ctx, _selectUserShareToken, mid)
  20. if err != nil {
  21. log.Errorv(ctx, log.KV("GetUserShareToken", err))
  22. return ""
  23. }
  24. var token string
  25. for rows.Next() {
  26. rows.Scan(&token)
  27. }
  28. return token
  29. }
  30. return string(raw)
  31. }
  32. // SetUserShareToken .
  33. func (d *Dao) SetUserShareToken(ctx context.Context, mid int64, token string) (int64, error) {
  34. result, err := d.db.Exec(ctx, _insertUserShareToken, mid, token)
  35. fmt.Println(result, err)
  36. if err != nil {
  37. return 0, err
  38. }
  39. if n, _ := result.RowsAffected(); n > 0 {
  40. conn := d.redis.Get(ctx)
  41. defer conn.Close()
  42. conn.Do("SET", fmt.Sprintf(model.CacheKeyUserShareToken, mid), token)
  43. }
  44. return result.LastInsertId()
  45. }