Tokey.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package Account
  2. import (
  3. "ColdVerify_local/conf"
  4. "fmt"
  5. "github.com/astaxie/beego/cache"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. uuid "github.com/satori/go.uuid"
  8. "time"
  9. )
  10. var redisCache_Tokey cache.Cache
  11. func init() {
  12. //注册模型
  13. //orm.RegisterModel(new(Tokey))
  14. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  15. "redis_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  16. fmt.Println(config)
  17. var err error
  18. redisCache_Tokey, err = cache.NewCache("redis", config)
  19. if err != nil || redisCache_Tokey == nil {
  20. errMsg := "failed to init redis"
  21. fmt.Println(errMsg, err)
  22. }
  23. }
  24. // ---------------- Redis -------------------
  25. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  26. func Redis_Tokey_Set(key string, r string) (err error) {
  27. err = redisCache_Tokey.Put(key, r, 24*time.Hour)
  28. if err != nil {
  29. fmt.Println("set key:", key, ",value:", r, err)
  30. }
  31. return
  32. }
  33. //if r,is :=Redis_Get(T_sn);is{
  34. //return r,nil
  35. //}
  36. func Redis_Tokey_Get(key string) (r string, is bool) {
  37. if redisCache_Tokey.IsExist(key) {
  38. //println("找到key:",key)
  39. v := redisCache_Tokey.Get(key)
  40. value := string(v.([]byte))
  41. return value, true
  42. }
  43. //println("没有 找到key:",key)
  44. return "", false
  45. }
  46. func Redis_DelK(key string) (err error) {
  47. err = redisCache_Tokey.Delete(key)
  48. return
  49. }
  50. // ---------------- 特殊方法 -------------------
  51. // 验证 TOKEY
  52. func Read_Tokey(User_tokey string) (string, bool) {
  53. return Redis_Tokey_Get(User_tokey)
  54. }
  55. // 添加 Tokey
  56. func Add_Tokey(User_uuid string) (Tokey string) {
  57. for true {
  58. Tokey = uuid.NewV4().String()
  59. _, is := Redis_Tokey_Get(Tokey)
  60. if !is {
  61. break
  62. }
  63. fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
  64. }
  65. Redis_Tokey_Set(Tokey, User_uuid)
  66. return Tokey
  67. }