登录缓存储缓存
This commit is contained in:
parent
b107b88800
commit
793a3d460a
@ -6,14 +6,7 @@
|
|||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="2037df1b-9ccc-4caa-9145-2d6722712612" name="更改" comment="">
|
<list default="true" id="2037df1b-9ccc-4caa-9145-2d6722712612" name="更改" comment="">
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/center/admincenter/internal/admin/api.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/admincenter/internal/admin/api.go" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/center/usercenter/internal/user/logic.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/usercenter/internal/user/logic.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/center/admincenter/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/admincenter/main.go" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/center/common/remark/remarkMgr.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/common/remark/remarkMgr.go" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/center/common/webServer/reflect.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/common/webServer/reflect.go" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/center/common/webServer/serverMux.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/common/webServer/serverMux.go" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/center/common/webServer/tokenHandler.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/common/webServer/tokenHandler.go" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/center/usercenter/internal/game/api.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/usercenter/internal/game/api.go" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/center/usercenter/internal/user/api.go" beforeDir="false" afterPath="$PROJECT_DIR$/center/usercenter/internal/user/api.go" afterDir="false" />
|
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@ -44,27 +37,28 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent">{
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"DefaultGoTemplateProperty": "Go File",
|
"DefaultGoTemplateProperty": "Go File",
|
||||||
"Docker.admincenter.redis: Compose 部署.executor": "Run",
|
"Docker.admincenter.redis: Compose 部署.executor": "Run",
|
||||||
"Docker.center/admincenter/Dockerfile builder.executor": "Run",
|
"Docker.center/admincenter/Dockerfile builder.executor": "Run",
|
||||||
"Go 构建.go build admincenter.executor": "Debug",
|
"Go 构建.go build admincenter.executor": "Debug",
|
||||||
"Go 构建.go build logincenter.executor": "Debug",
|
"Go 构建.go build logincenter.executor": "Debug",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"Go 构建.go build usercenter.executor": "Debug",
|
||||||
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
||||||
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
||||||
"git-widget-placeholder": "master",
|
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
||||||
"go.import.settings.migrated": "true",
|
"git-widget-placeholder": "master",
|
||||||
"go.sdk.automatically.set": "true",
|
"go.import.settings.migrated": "true",
|
||||||
"last_opened_file_path": "D:/workspace/e2023/goProject/trunk",
|
"go.sdk.automatically.set": "true",
|
||||||
"node.js.detected.package.eslint": "true",
|
"last_opened_file_path": "D:/workspace/e2023/goProject/trunk",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.detected.package.eslint": "true",
|
||||||
"nodejs_package_manager_path": "npm",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"settings.editor.selected.configurable": "preferences.pluginManager"
|
"nodejs_package_manager_path": "npm",
|
||||||
|
"settings.editor.selected.configurable": "preferences.pluginManager"
|
||||||
}
|
}
|
||||||
}</component>
|
}]]></component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
<recent name="D:\workspace\e2023\goProject\trunk\center\logincenter\internal\game" />
|
<recent name="D:\workspace\e2023\goProject\trunk\center\logincenter\internal\game" />
|
||||||
@ -72,7 +66,7 @@
|
|||||||
<recent name="D:\workspace\e2023\goProject\trunk\center" />
|
<recent name="D:\workspace\e2023\goProject\trunk\center" />
|
||||||
</key>
|
</key>
|
||||||
</component>
|
</component>
|
||||||
<component name="RunManager" selected="Go 构建.go build admincenter">
|
<component name="RunManager" selected="Go 构建.go build usercenter">
|
||||||
<configuration name="go build admincenter" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
|
<configuration name="go build admincenter" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
|
||||||
<module name="trunk" />
|
<module name="trunk" />
|
||||||
<working_directory value="$PROJECT_DIR$/center/admincenter" />
|
<working_directory value="$PROJECT_DIR$/center/admincenter" />
|
||||||
@ -82,9 +76,9 @@
|
|||||||
<filePath value="$PROJECT_DIR$/center/admincenter/main.go" />
|
<filePath value="$PROJECT_DIR$/center/admincenter/main.go" />
|
||||||
<method v="2" />
|
<method v="2" />
|
||||||
</configuration>
|
</configuration>
|
||||||
<configuration name="go build logincenter" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
|
<configuration name="go build usercenter" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true">
|
||||||
<module name="trunk" />
|
<module name="trunk" />
|
||||||
<working_directory value="$PROJECT_DIR$/center/logincenter" />
|
<working_directory value="$PROJECT_DIR$/center/usercenter" />
|
||||||
<kind value="PACKAGE" />
|
<kind value="PACKAGE" />
|
||||||
<package value="logincenter" />
|
<package value="logincenter" />
|
||||||
<directory value="$PROJECT_DIR$" />
|
<directory value="$PROJECT_DIR$" />
|
||||||
@ -128,8 +122,8 @@
|
|||||||
</configuration>
|
</configuration>
|
||||||
<recent_temporary>
|
<recent_temporary>
|
||||||
<list>
|
<list>
|
||||||
|
<item itemvalue="Go 构建.go build usercenter" />
|
||||||
<item itemvalue="Go 构建.go build admincenter" />
|
<item itemvalue="Go 构建.go build admincenter" />
|
||||||
<item itemvalue="Go 构建.go build logincenter" />
|
|
||||||
<item itemvalue="Docker.admincenter.redis: Compose 部署" />
|
<item itemvalue="Docker.admincenter.redis: Compose 部署" />
|
||||||
<item itemvalue="Docker.center/admincenter/Dockerfile builder" />
|
<item itemvalue="Docker.center/admincenter/Dockerfile builder" />
|
||||||
</list>
|
</list>
|
||||||
@ -158,6 +152,11 @@
|
|||||||
<line>68</line>
|
<line>68</line>
|
||||||
<option name="timeStamp" value="8" />
|
<option name="timeStamp" value="8" />
|
||||||
</line-breakpoint>
|
</line-breakpoint>
|
||||||
|
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
|
||||||
|
<url>file://$PROJECT_DIR$/center/usercenter/internal/user/logic.go</url>
|
||||||
|
<line>104</line>
|
||||||
|
<option name="timeStamp" value="23" />
|
||||||
|
</line-breakpoint>
|
||||||
</breakpoints>
|
</breakpoints>
|
||||||
</breakpoint-manager>
|
</breakpoint-manager>
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
@ -7,9 +7,13 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 用户缓存对象
|
var (
|
||||||
var userMap = make(map[int64]*User)
|
// 用户缓存对象
|
||||||
var rwmu sync.RWMutex
|
userMap = make(map[int64]*User)
|
||||||
|
rwmu sync.RWMutex
|
||||||
|
userAccountMap = make(map[string]*User)
|
||||||
|
rwmu2 sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
// GetUserByID 根据用户id获取用户信息
|
// GetUserByID 根据用户id获取用户信息
|
||||||
func GetUserByID(UserID int64) (*User, error) {
|
func GetUserByID(UserID int64) (*User, error) {
|
||||||
@ -55,35 +59,61 @@ func AddUserCache(user *User) {
|
|||||||
userMap[user.ID] = user
|
userMap[user.ID] = user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUserByAccount(account string) (*User, error) {
|
||||||
|
rwmu2.RLock()
|
||||||
|
defer rwmu2.RUnlock()
|
||||||
|
|
||||||
|
//判断缓存是否存在
|
||||||
|
var user *User
|
||||||
|
ok := true
|
||||||
|
if user, ok = userAccountMap[account]; ok {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddUserIdCache(user *User) {
|
||||||
|
rwmu2.Lock()
|
||||||
|
defer rwmu2.Unlock()
|
||||||
|
user.Cache = cache.NewCache()
|
||||||
|
userAccountMap[user.Account] = user
|
||||||
|
}
|
||||||
|
|
||||||
// AddUser 添加用户
|
// AddUser 添加用户
|
||||||
// AddUser 添加新的用户到数据库中。
|
// AddUser 添加新的用户到数据库中。
|
||||||
// 参数 User: 包含用户信息的对象。
|
// 参数 User: 包含用户信息的对象。
|
||||||
// 返回值: 插入操作影响的行数和可能发生的错误。
|
// 返回值: 插入操作影响的行数和可能发生的错误。
|
||||||
func AddUser(User *User) (int64, error) {
|
func AddUser(user *User) (int64, error) {
|
||||||
|
|
||||||
//处理一些验证
|
//处理一些验证
|
||||||
|
|
||||||
//写入缓存
|
|
||||||
AddUserCache(User)
|
|
||||||
|
|
||||||
// 写入到数据库
|
// 写入到数据库
|
||||||
result := connection.GetUserDB().Create(&User) // 通过数据的指针来创建
|
result := connection.GetUserDB().Create(&user) // 通过数据的指针来创建
|
||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
logUtilPlus.ErrorLog("添加用户失败 错误信息:", result.Error.Error())
|
logUtilPlus.ErrorLog("添加用户失败 错误信息:", result.Error.Error())
|
||||||
}
|
}
|
||||||
return User.ID, nil
|
|
||||||
|
return user.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login 用户登录
|
// Login 用户登录
|
||||||
func Login(account string, password string) (*User, error) {
|
func Login(account string, password string) (*User, error) {
|
||||||
|
|
||||||
//这里优先验证缓存数据
|
var user *User
|
||||||
|
|
||||||
var User User
|
//这里优先验证缓存数据
|
||||||
result := connection.GetUserDB().Where("account = ? AND password = ?", account, password).First(&User)
|
user, err := GetUserByAccount(account)
|
||||||
|
if err == nil && user != nil {
|
||||||
|
return user, err
|
||||||
|
}
|
||||||
|
result := connection.GetUserDB().Where("account = ? AND password = ?", account, password).First(&user)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
return &User, nil
|
|
||||||
|
//写入缓存
|
||||||
|
AddUserCache(user)
|
||||||
|
AddUserIdCache(user)
|
||||||
|
|
||||||
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user