mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-11-25 19:37:41 +08:00
* feat(cache): improve cache management * feat(disk-usage): add cache * feat(disk-usage): add refresh * fix(disk-usage): cache with ttl * feat(cache): implement KeyedCache and TypedCache for improved caching mechanism * fix(copy): update object retrieval to use Get instead of GetUnwrap * refactor(cache): simplify DirectoryCache structure and improve object management * fix(cache): correct cache entry initialization and key deletion logic in TypedCache * refactor(driver): remove GetObjInfo interface and simplify Link function logic https://github.com/OpenListTeam/OpenList/pull/888/files#r2430925783 * fix(link): optimize link retrieval and caching logic * refactor(cache): consolidate cache management and improve directory cache handling * fix(cache): add cache control based on storage configuration in List function * . * refactor: replace fmt.Sprintf with strconv for integer conversions * refactor(cache): enhance cache entry management with Expirable interface * fix(cache): improve link reference acquisition logic to handle expiration * refactor: replace OnlyLinkMFile with NoLinkSF in driver configurations and logic * refactor(link): enhance link caching logic with dynamic type keys based on IP and User-Agent * feat(drivers): add LinkCacheType to driver configurations for enhanced caching * refactor(cache): streamline directory object management in cache operations * refactor(cache): remove unnecessary 'dirty' field from CacheEntry structure * refactor(cache): replace 'dirty' field with bitwise flags * refactor(io): 调高SyncClosers.AcquireReference的优先级 * refactor(link): 优化链接获取逻辑,增加重 * refactor(link): 添加RequireReference字段以增强链接管理 * refactor(link): 移除MFile字段,改用RangeReader * refactor: 移除不必要的NoLinkSF字段 * refactor(cache): 修改目录缓存的脏标志定义和更新逻辑 * feat(cache): add expiration gc --------- Co-authored-by: KirCute <951206789@qq.com> Co-authored-by: KirCute <kircute@foxmail.com> Co-authored-by: j2rong4cn <j2rong@qq.com>
127 lines
2.5 KiB
Go
127 lines
2.5 KiB
Go
package op
|
|
|
|
import (
|
|
"github.com/OpenListTeam/OpenList/v4/internal/db"
|
|
"github.com/OpenListTeam/OpenList/v4/internal/errs"
|
|
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
|
"github.com/OpenListTeam/OpenList/v4/pkg/singleflight"
|
|
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
|
|
)
|
|
|
|
var userG singleflight.Group[*model.User]
|
|
var guestUser *model.User
|
|
var adminUser *model.User
|
|
|
|
func GetAdmin() (*model.User, error) {
|
|
if adminUser == nil {
|
|
user, err := db.GetUserByRole(model.ADMIN)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
adminUser = user
|
|
}
|
|
return adminUser, nil
|
|
}
|
|
|
|
func GetGuest() (*model.User, error) {
|
|
if guestUser == nil {
|
|
user, err := db.GetUserByRole(model.GUEST)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
guestUser = user
|
|
}
|
|
return guestUser, nil
|
|
}
|
|
|
|
func GetUserByRole(role int) (*model.User, error) {
|
|
return db.GetUserByRole(role)
|
|
}
|
|
|
|
func GetUserByName(username string) (*model.User, error) {
|
|
if username == "" {
|
|
return nil, errs.EmptyUsername
|
|
}
|
|
if user, exists := Cache.GetUser(username); exists {
|
|
return user, nil
|
|
}
|
|
user, err, _ := userG.Do(username, func() (*model.User, error) {
|
|
_user, err := db.GetUserByName(username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
Cache.SetUser(username, _user)
|
|
return _user, nil
|
|
})
|
|
return user, err
|
|
}
|
|
|
|
func GetUserById(id uint) (*model.User, error) {
|
|
return db.GetUserById(id)
|
|
}
|
|
|
|
func GetUsers(pageIndex, pageSize int) (users []model.User, count int64, err error) {
|
|
return db.GetUsers(pageIndex, pageSize)
|
|
}
|
|
|
|
func CreateUser(u *model.User) error {
|
|
u.BasePath = utils.FixAndCleanPath(u.BasePath)
|
|
return db.CreateUser(u)
|
|
}
|
|
|
|
func DeleteUserById(id uint) error {
|
|
old, err := db.GetUserById(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if old.IsAdmin() || old.IsGuest() {
|
|
return errs.DeleteAdminOrGuest
|
|
}
|
|
Cache.DeleteUser(old.Username)
|
|
return db.DeleteUserById(id)
|
|
}
|
|
|
|
func UpdateUser(u *model.User) error {
|
|
old, err := db.GetUserById(u.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if u.IsAdmin() {
|
|
adminUser = nil
|
|
}
|
|
if u.IsGuest() {
|
|
guestUser = nil
|
|
}
|
|
Cache.DeleteUser(old.Username)
|
|
u.BasePath = utils.FixAndCleanPath(u.BasePath)
|
|
return db.UpdateUser(u)
|
|
}
|
|
|
|
func Cancel2FAByUser(u *model.User) error {
|
|
u.OtpSecret = ""
|
|
return UpdateUser(u)
|
|
}
|
|
|
|
func Cancel2FAById(id uint) error {
|
|
user, err := db.GetUserById(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Cancel2FAByUser(user)
|
|
}
|
|
|
|
func DelUserCache(username string) error {
|
|
user, err := GetUserByName(username)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if user.IsAdmin() {
|
|
adminUser = nil
|
|
}
|
|
if user.IsGuest() {
|
|
guestUser = nil
|
|
}
|
|
Cache.DeleteUser(username)
|
|
return nil
|
|
}
|