2
0

user_cache.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package model
  2. import (
  3. "fmt"
  4. "one-api/common"
  5. "one-api/constant"
  6. "strconv"
  7. "time"
  8. )
  9. // Change UserCache struct to userCache
  10. type userCache struct {
  11. Id int `json:"id"`
  12. Group string `json:"group"`
  13. Quota int `json:"quota"`
  14. Status int `json:"status"`
  15. Role int `json:"role"`
  16. Username string `json:"username"`
  17. }
  18. // Rename all exported functions to private ones
  19. // invalidateUserCache clears all user related cache
  20. func invalidateUserCache(userId int) error {
  21. if !common.RedisEnabled {
  22. return nil
  23. }
  24. keys := []string{
  25. fmt.Sprintf(constant.UserGroupKeyFmt, userId),
  26. fmt.Sprintf(constant.UserQuotaKeyFmt, userId),
  27. fmt.Sprintf(constant.UserEnabledKeyFmt, userId),
  28. fmt.Sprintf(constant.UserUsernameKeyFmt, userId),
  29. }
  30. for _, key := range keys {
  31. if err := common.RedisDel(key); err != nil {
  32. return fmt.Errorf("failed to delete cache key %s: %w", key, err)
  33. }
  34. }
  35. return nil
  36. }
  37. // updateUserGroupCache updates user group cache
  38. func updateUserGroupCache(userId int, group string) error {
  39. if !common.RedisEnabled {
  40. return nil
  41. }
  42. return common.RedisSet(
  43. fmt.Sprintf(constant.UserGroupKeyFmt, userId),
  44. group,
  45. time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second,
  46. )
  47. }
  48. // updateUserQuotaCache updates user quota cache
  49. func updateUserQuotaCache(userId int, quota int) error {
  50. if !common.RedisEnabled {
  51. return nil
  52. }
  53. return common.RedisSet(
  54. fmt.Sprintf(constant.UserQuotaKeyFmt, userId),
  55. fmt.Sprintf("%d", quota),
  56. time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second,
  57. )
  58. }
  59. // updateUserStatusCache updates user status cache
  60. func updateUserStatusCache(userId int, userEnabled bool) error {
  61. if !common.RedisEnabled {
  62. return nil
  63. }
  64. enabled := "0"
  65. if userEnabled {
  66. enabled = "1"
  67. }
  68. return common.RedisSet(
  69. fmt.Sprintf(constant.UserEnabledKeyFmt, userId),
  70. enabled,
  71. time.Duration(constant.UserId2StatusCacheSeconds)*time.Second,
  72. )
  73. }
  74. // updateUserNameCache updates username cache
  75. func updateUserNameCache(userId int, username string) error {
  76. if !common.RedisEnabled {
  77. return nil
  78. }
  79. return common.RedisSet(
  80. fmt.Sprintf(constant.UserUsernameKeyFmt, userId),
  81. username,
  82. time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second,
  83. )
  84. }
  85. // updateUserCache updates all user cache fields
  86. func updateUserCache(userId int, username string, userGroup string, quota int, status int) error {
  87. if !common.RedisEnabled {
  88. return nil
  89. }
  90. if err := updateUserGroupCache(userId, userGroup); err != nil {
  91. return fmt.Errorf("update group cache: %w", err)
  92. }
  93. if err := updateUserQuotaCache(userId, quota); err != nil {
  94. return fmt.Errorf("update quota cache: %w", err)
  95. }
  96. if err := updateUserStatusCache(userId, status == common.UserStatusEnabled); err != nil {
  97. return fmt.Errorf("update status cache: %w", err)
  98. }
  99. if err := updateUserNameCache(userId, username); err != nil {
  100. return fmt.Errorf("update username cache: %w", err)
  101. }
  102. return nil
  103. }
  104. // getUserGroupCache gets user group from cache
  105. func getUserGroupCache(userId int) (string, error) {
  106. if !common.RedisEnabled {
  107. return "", nil
  108. }
  109. return common.RedisGet(fmt.Sprintf(constant.UserGroupKeyFmt, userId))
  110. }
  111. // getUserQuotaCache gets user quota from cache
  112. func getUserQuotaCache(userId int) (int, error) {
  113. if !common.RedisEnabled {
  114. return 0, nil
  115. }
  116. quotaStr, err := common.RedisGet(fmt.Sprintf(constant.UserQuotaKeyFmt, userId))
  117. if err != nil {
  118. return 0, err
  119. }
  120. return strconv.Atoi(quotaStr)
  121. }
  122. // getUserStatusCache gets user status from cache
  123. func getUserStatusCache(userId int) (int, error) {
  124. if !common.RedisEnabled {
  125. return 0, nil
  126. }
  127. statusStr, err := common.RedisGet(fmt.Sprintf(constant.UserEnabledKeyFmt, userId))
  128. if err != nil {
  129. return 0, err
  130. }
  131. return strconv.Atoi(statusStr)
  132. }
  133. // getUserNameCache gets username from cache
  134. func getUserNameCache(userId int) (string, error) {
  135. if !common.RedisEnabled {
  136. return "", nil
  137. }
  138. return common.RedisGet(fmt.Sprintf(constant.UserUsernameKeyFmt, userId))
  139. }
  140. // getUserCache gets complete user cache
  141. func getUserCache(userId int) (*userCache, error) {
  142. if !common.RedisEnabled {
  143. return nil, nil
  144. }
  145. group, err := getUserGroupCache(userId)
  146. if err != nil {
  147. return nil, fmt.Errorf("get group cache: %w", err)
  148. }
  149. quota, err := getUserQuotaCache(userId)
  150. if err != nil {
  151. return nil, fmt.Errorf("get quota cache: %w", err)
  152. }
  153. status, err := getUserStatusCache(userId)
  154. if err != nil {
  155. return nil, fmt.Errorf("get status cache: %w", err)
  156. }
  157. username, err := getUserNameCache(userId)
  158. if err != nil {
  159. return nil, fmt.Errorf("get username cache: %w", err)
  160. }
  161. return &userCache{
  162. Id: userId,
  163. Group: group,
  164. Quota: quota,
  165. Status: status,
  166. Username: username,
  167. }, nil
  168. }
  169. // Add atomic quota operations
  170. func cacheIncrUserQuota(userId int, delta int64) error {
  171. if !common.RedisEnabled {
  172. return nil
  173. }
  174. key := fmt.Sprintf(constant.UserQuotaKeyFmt, userId)
  175. return common.RedisIncr(key, delta)
  176. }
  177. func cacheDecrUserQuota(userId int, delta int64) error {
  178. return cacheIncrUserQuota(userId, -delta)
  179. }