cache.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package dao
  2. import (
  3. "DataShare/global"
  4. "DataShare/model"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/sirupsen/logrus"
  9. "strconv"
  10. "time"
  11. )
  12. //Save token metadata to Redis
  13. func CreateAuth(certKey string, td *model.TokenDetails) error {
  14. tokenExpire := global.SystemConfig.Redis.Cache.TokenExpire
  15. atCreated, err := global.RedisClient.Set(global.RedisDataShare+":"+"auth:"+td.TokenUuid, certKey, time.Second * time.Duration(tokenExpire)).Result()
  16. if err != nil {
  17. return err
  18. }
  19. refrushTokenExpire := global.SystemConfig.Redis.Cache.RefrushTokenExpire
  20. rtCreated, err := global.RedisClient.Set(global.RedisDataShare+":"+"auth:"+td.RefreshUuid, certKey, time.Second*time.Duration(refrushTokenExpire)).Result()
  21. if err != nil {
  22. return err
  23. }
  24. if atCreated == "0" || rtCreated == "0" {
  25. return errors.New("no record inserted")
  26. }
  27. return nil
  28. }
  29. //Check the metadata saved
  30. func FetchAuth(tokenUuid string) (string, error) {
  31. userid, err := global.RedisClient.Get(global.RedisDataShare+":"+"auth:"+tokenUuid).Result()
  32. if err != nil {
  33. return "", err
  34. }
  35. return userid, nil
  36. }
  37. //Once a user row in the token table
  38. func DeleteTokens(authD *model.AccessDetails) error {
  39. //get the refresh uuid
  40. refreshUuid := fmt.Sprintf("%s++%s", authD.TokenUuid, authD.AppId)
  41. //delete access token
  42. deletedAt, err := global.RedisClient.Del(global.RedisDataShare+":"+"auth:"+authD.TokenUuid).Result()
  43. if err != nil {
  44. return err
  45. }
  46. //delete refresh token
  47. deletedRt, err := global.RedisClient.Del(global.RedisDataShare+":"+"auth:"+refreshUuid).Result()
  48. if err != nil {
  49. return err
  50. }
  51. //When the record is deleted, the return value is 1
  52. if deletedAt != 1 || deletedRt != 1 {
  53. return errors.New("something went wrong")
  54. }
  55. return nil
  56. }
  57. func DeleteRefresh(refreshUuid string) error {
  58. //delete refresh token
  59. deleted, err := global.RedisClient.Del(global.RedisDataShare+":"+"auth:"+refreshUuid).Result()
  60. if err != nil || deleted == 0 {
  61. return err
  62. }
  63. return nil
  64. }
  65. func DeleteTokenByUserNameAndType(appId string,appSecret string) {
  66. sliceCmd := global.RedisClient.Keys(global.RedisDataShare+":"+"auth:"+"uuid_*")
  67. if sliceCmd == nil {
  68. return
  69. }
  70. val := sliceCmd.Val()
  71. for i:=0;i<len(val);i++ {
  72. res,_:= global.RedisClient.Get(val[i]).Result()
  73. var uuidVal model.UuidValue
  74. if err := json.Unmarshal([]byte(res),&uuidVal);err ==nil {
  75. if appId==uuidVal.AppId && appSecret==uuidVal.AppSecret{
  76. //删除记录
  77. global.RedisClient.Del(val[i]).Result()
  78. }
  79. }
  80. }
  81. }
  82. //
  83. func LoadConnectInfo()([]model.DbConnInfo) {
  84. conn_array := make([]model.DbConnInfo,0)
  85. conn_list := global.RedisClient.LRange ("database",0,1000).Val()
  86. for i:=0;i<len(conn_list);i++{
  87. fmt.Println(conn_list[i])
  88. var conn_info model.DbConnInfo
  89. if err := json.Unmarshal([]byte(conn_list[i]),&conn_info);err ==nil {
  90. conn_array = append(conn_array,conn_info)
  91. }else{
  92. global.SystemLogger.Log(logrus.ErrorLevel,"load connection config failed,"+err.Error())
  93. }
  94. }
  95. return conn_array
  96. }
  97. //加载数据缓存参数
  98. func LoadDataCacheList()([]model.DataCacheOfRedis){
  99. data_cache_list := make([]model.DataCacheOfRedis,0)
  100. cache_list := global.RedisClient.LRange ("datacache",0,1000).Val()
  101. for i:=0;i<len(cache_list);i++{
  102. var item_info model.DataCacheOfRedis
  103. if err := json.Unmarshal([]byte(cache_list[i]),&item_info);err ==nil {
  104. data_cache_list = append(data_cache_list,item_info)
  105. }else{
  106. global.SystemLogger.Log(logrus.ErrorLevel,"load data cache failed,"+err.Error())
  107. }
  108. }
  109. return data_cache_list
  110. }
  111. //获取接口调用次数
  112. func GetApiCallCount(accessId string)(int){
  113. key := global.RedisDataShare +":"+"dayLimit"+":"+"dayLimit_"+accessId
  114. exist := global.RedisClient.Exists(key).Val()
  115. if exist == 0 {
  116. return 0
  117. }
  118. ret := global.RedisClient.Get(key).Val()
  119. cnt,_ := strconv.Atoi(ret)
  120. return cnt
  121. }
  122. //自增接口调用次数
  123. func IncApiCallCount(accessId string)(error){
  124. key := global.RedisDataShare +":"+"dayLimit"+":"+"dayLimit_"+accessId
  125. count := GetApiCallCount(accessId)
  126. var year,month,day,hour,minute,second int
  127. currentTime := time.Now().Format("2006-01-02 15:04:05")
  128. _,_= fmt.Sscanf(currentTime,"%4d-%2d-%2d %2d:%2d:%2d",&year,&month,&day,&hour,&minute,&second)
  129. hh := 23-hour
  130. mm := 59-minute
  131. ss := 59 - second
  132. insert, err := global.RedisClient.Set(key, count+1, time.Second*time.Duration(hh*3600 + mm*60 + ss)).Result()
  133. if err != nil {
  134. return err
  135. }
  136. if insert == "0" {
  137. return errors.New("no record inserted")
  138. }
  139. return nil
  140. }