init_user.go 535 B

123456789101112131415161718192021222324252627
  1. package user
  2. import (
  3. "github.com/0xJacky/Nginx-UI/model"
  4. "github.com/gin-gonic/gin"
  5. "github.com/uozi-tech/cosy"
  6. )
  7. // GetInitUser get the init user from database with caching
  8. func GetInitUser(c *gin.Context) *model.User {
  9. // Try to get from cache first
  10. if cachedUser, found := GetCachedUser(1); found {
  11. return cachedUser
  12. }
  13. // If not in cache, get from database
  14. db := cosy.UseDB(c)
  15. user := &model.User{}
  16. db.First(user, 1)
  17. // Cache the user for future requests
  18. if user.ID != 0 {
  19. CacheUser(user)
  20. }
  21. return user
  22. }