Tokey.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package Account
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/logs"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. uuid "github.com/satori/go.uuid"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. var redisCache_Tokey cache.Cache
  14. func init() {
  15. //注册模型
  16. //orm.RegisterModel(new(Tokey))
  17. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  18. "redis_User_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  19. fmt.Println(config)
  20. var err error
  21. redisCache_Tokey, err = cache.NewCache("redis", config)
  22. if err != nil || redisCache_Tokey == nil {
  23. logs.Error("Redis Tokey init err", 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. err := redisCache_Tokey.Put(Tokey, Uuid+"|0", 2*time.Hour)
  38. if err != nil {
  39. logs.Error("Add_Tokey_Set", err)
  40. }
  41. return Tokey
  42. }
  43. // if r,is :=Redis_Get(T_sn);is{
  44. // return r,nil
  45. // }
  46. func Redis_Tokey_Get(Tokey string) (string, bool) {
  47. if len(Tokey) < 10 {
  48. return "", false
  49. }
  50. if redisCache_Tokey.IsExist(Tokey) {
  51. //println("找到key:",key)
  52. v := redisCache_Tokey.Get(Tokey)
  53. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  54. err := redisCache_Tokey.Put(Tokey, value, 2*time.Hour) // 重新计次
  55. if err != nil {
  56. logs.Error("Redis_Tokey_Get", err)
  57. }
  58. return value, true
  59. }
  60. //println("没有 找到key:",key)
  61. return "", false
  62. }
  63. func Redis_Tokey_T_pid_Set(Tokey string, T_pid int) bool {
  64. if len(Tokey) < 10 {
  65. return false
  66. }
  67. if redisCache_Tokey.IsExist(Tokey) {
  68. //println("找到key:",key)
  69. v := redisCache_Tokey.Get(Tokey)
  70. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  71. T_uuid := strings.Split(value, "|")[0]
  72. err := redisCache_Tokey.Put(Tokey, T_uuid+"|"+strconv.Itoa(T_pid), 2*time.Hour) // 重新计次
  73. if err != nil {
  74. logs.Error("Redis_Tokey_T_pid_Set", err)
  75. }
  76. return true
  77. }
  78. return false
  79. }
  80. func Redis_Tokey_T_pid_Get(Tokey string) (int, bool) {
  81. if len(Tokey) < 10 {
  82. return 0, false
  83. }
  84. if redisCache_Tokey.IsExist(Tokey) {
  85. //println("找到key:",key)
  86. v := redisCache_Tokey.Get(Tokey)
  87. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  88. T_pid, _ := strconv.Atoi(strings.Split(value, "|")[1])
  89. return T_pid, true
  90. }
  91. return 0, false
  92. }