GetUserCache gets complete user cache from hash
(userId int)
| 78 | |
| 79 | // GetUserCache gets complete user cache from hash |
| 80 | func GetUserCache(userId int) (userCache *UserBase, err error) { |
| 81 | var user *User |
| 82 | var fromDB bool |
| 83 | defer func() { |
| 84 | // Update Redis cache asynchronously on successful DB read |
| 85 | if shouldUpdateRedis(fromDB, err) && user != nil { |
| 86 | gopool.Go(func() { |
| 87 | if err := updateUserCache(*user); err != nil { |
| 88 | common.SysLog("failed to update user status cache: " + err.Error()) |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | }() |
| 93 | |
| 94 | // Try getting from Redis first |
| 95 | userCache, err = cacheGetUserBase(userId) |
| 96 | if err == nil { |
| 97 | return userCache, nil |
| 98 | } |
| 99 | |
| 100 | // If Redis fails, get from DB |
| 101 | fromDB = true |
| 102 | user, err = GetUserById(userId, false) |
| 103 | if err != nil { |
| 104 | return nil, err // Return nil and error if DB lookup fails |
| 105 | } |
| 106 | |
| 107 | // Create cache object from user data |
| 108 | userCache = &UserBase{ |
| 109 | Id: user.Id, |
| 110 | Group: user.Group, |
| 111 | Quota: user.Quota, |
| 112 | Status: user.Status, |
| 113 | Username: user.Username, |
| 114 | Setting: user.Setting, |
| 115 | Email: user.Email, |
| 116 | } |
| 117 | |
| 118 | return userCache, nil |
| 119 | } |
| 120 | |
| 121 | func cacheGetUserBase(userId int) (*UserBase, error) { |
| 122 | if !common.RedisEnabled { |
no test coverage detected