cache.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "one-api/common"
  6. "sync"
  7. "time"
  8. )
  9. const (
  10. TokenCacheSeconds = 60 * 60
  11. UserId2GroupCacheSeconds = 60 * 60
  12. )
  13. func CacheGetTokenByKey(key string) (*Token, error) {
  14. var token Token
  15. if !common.RedisEnabled {
  16. err := DB.Where("`key` = ?", key).First(&token).Error
  17. return &token, err
  18. }
  19. tokenObjectString, err := common.RedisGet(fmt.Sprintf("token:%s", key))
  20. if err != nil {
  21. err := DB.Where("`key` = ?", key).First(&token).Error
  22. if err != nil {
  23. return nil, err
  24. }
  25. jsonBytes, err := json.Marshal(token)
  26. if err != nil {
  27. return nil, err
  28. }
  29. err = common.RedisSet(fmt.Sprintf("token:%s", key), string(jsonBytes), TokenCacheSeconds*time.Second)
  30. if err != nil {
  31. common.SysError("Redis set token error: " + err.Error())
  32. }
  33. return &token, nil
  34. }
  35. err = json.Unmarshal([]byte(tokenObjectString), &token)
  36. return &token, err
  37. }
  38. func CacheGetUserGroup(id int) (group string, err error) {
  39. if !common.RedisEnabled {
  40. return GetUserGroup(id)
  41. }
  42. group, err = common.RedisGet(fmt.Sprintf("user_group:%d", id))
  43. if err != nil {
  44. group, err = GetUserGroup(id)
  45. if err != nil {
  46. return "", err
  47. }
  48. err = common.RedisSet(fmt.Sprintf("user_group:%d", id), group, UserId2GroupCacheSeconds*time.Second)
  49. if err != nil {
  50. common.SysError("Redis set user group error: " + err.Error())
  51. }
  52. }
  53. return group, err
  54. }
  55. var channelId2channel map[int]*Channel
  56. var channelSyncLock sync.RWMutex
  57. var group2model2channels map[string]map[string][]*Channel
  58. func InitChannelCache() {
  59. channelSyncLock.Lock()
  60. defer channelSyncLock.Unlock()
  61. channelId2channel = make(map[int]*Channel)
  62. var channels []*Channel
  63. DB.Find(&channels)
  64. for _, channel := range channels {
  65. channelId2channel[channel.Id] = channel
  66. }
  67. var abilities []*Ability
  68. DB.Find(&abilities)
  69. groups := make(map[string]bool)
  70. for _, ability := range abilities {
  71. groups[ability.Group] = true
  72. }
  73. group2model2channels = make(map[string]map[string][]*Channel)
  74. for group := range groups {
  75. group2model2channels[group] = make(map[string][]*Channel)
  76. // TODO: implement this
  77. }
  78. }
  79. func SyncChannelCache(frequency int) {
  80. for {
  81. time.Sleep(time.Duration(frequency) * time.Second)
  82. common.SysLog("Syncing channels from database")
  83. InitChannelCache()
  84. }
  85. }
  86. func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
  87. if !common.RedisEnabled {
  88. return GetRandomSatisfiedChannel(group, model)
  89. }
  90. return GetRandomSatisfiedChannel(group, model)
  91. // TODO: implement this
  92. return nil, nil
  93. }