Tokey.go 1.8 KB

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