Tokey.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package Account
  2. import (
  3. "Cold_Api/conf"
  4. "fmt"
  5. "github.com/astaxie/beego/cache"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. uuid "github.com/satori/go.uuid"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. var redisCache_Tokey cache.Cache
  13. func init() {
  14. //注册模型
  15. //orm.RegisterModel(new(Tokey))
  16. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  17. "redis_User_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  18. fmt.Println(config)
  19. var err error
  20. redisCache_Tokey, err = cache.NewCache("redis", config)
  21. if err != nil || redisCache_Tokey == nil {
  22. errMsg := "failed to init redis"
  23. fmt.Println(errMsg, err)
  24. }
  25. }
  26. // ---------------- Redis -------------------
  27. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  28. func Add_Tokey_Set(Uuid string) string {
  29. var Tokey string
  30. for true {
  31. Tokey = uuid.NewV4().String()
  32. if !redisCache_Tokey.IsExist(Tokey) {
  33. break
  34. }
  35. fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
  36. }
  37. redisCache_Tokey.Put(Tokey, Uuid+"|0", 2*time.Hour)
  38. return Tokey
  39. }
  40. // if r,is :=Redis_Get(T_sn);is{
  41. // return r,nil
  42. // }
  43. func Redis_Tokey_Get(Tokey string) (string, bool) {
  44. if len(Tokey) < 10 {
  45. return "", false
  46. }
  47. if redisCache_Tokey.IsExist(Tokey) {
  48. //println("找到key:",key)
  49. v := redisCache_Tokey.Get(Tokey)
  50. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  51. redisCache_Tokey.Put(Tokey, value, 2*time.Hour) // 重新计次
  52. return value, true
  53. }
  54. //println("没有 找到key:",key)
  55. return "", false
  56. }
  57. func Redis_Tokey_T_pid_Set(Tokey string, T_pid int) bool {
  58. if len(Tokey) < 10 {
  59. return false
  60. }
  61. if redisCache_Tokey.IsExist(Tokey) {
  62. //println("找到key:",key)
  63. v := redisCache_Tokey.Get(Tokey)
  64. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  65. T_uuid := strings.Split(value, "|")[0]
  66. redisCache_Tokey.Put(Tokey, T_uuid+"|"+strconv.Itoa(T_pid), 2*time.Hour) // 重新计次
  67. return true
  68. }
  69. return false
  70. }
  71. func Redis_Tokey_T_pid_Get(Tokey string) (int, bool) {
  72. if len(Tokey) < 10 {
  73. return 0, false
  74. }
  75. if redisCache_Tokey.IsExist(Tokey) {
  76. //println("找到key:",key)
  77. v := redisCache_Tokey.Get(Tokey)
  78. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  79. T_pid, _ := strconv.Atoi(strings.Split(value, "|")[1])
  80. return T_pid, true
  81. }
  82. return 0, false
  83. }