forked from free-lancers/electricity_bill_calc_service
feat(cache):实验完成缓存控制机制。
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/tools"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/fufuok/utils"
|
||||
@@ -114,8 +115,8 @@ func (u _UserService) InvalidUserPassword(uid string) (string, error) {
|
||||
}
|
||||
if affected > 0 {
|
||||
// ! 同一个用户在缓存中有两个键。
|
||||
cache.AbolishCacheData("user", user.Id)
|
||||
cache.AbolishCacheData("user", user.Username)
|
||||
cache.AbolishRelation(fmt.Sprintf("user_%s", uid))
|
||||
cache.AbolishRelation("user")
|
||||
return verifyCode, nil
|
||||
} else {
|
||||
return "", exceptions.NewUnsuccessfulOperationError()
|
||||
@@ -147,8 +148,8 @@ func (u _UserService) ResetUserPassword(username, password string) (bool, error)
|
||||
return false, err
|
||||
}
|
||||
if affected > 0 {
|
||||
cache.AbolishCacheData("user", user.Id)
|
||||
cache.AbolishCacheData("user", user.Username)
|
||||
cache.AbolishRelation(fmt.Sprintf("user_%s", user.Id))
|
||||
cache.AbolishRelation("user")
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
@@ -156,11 +157,25 @@ func (u _UserService) ResetUserPassword(username, password string) (bool, error)
|
||||
}
|
||||
|
||||
func (_UserService) IsUserExists(uid string) (bool, error) {
|
||||
return global.DBConn.ID(uid).Exist(&model.User{})
|
||||
if has, _ := cache.CheckExists("user", uid); has {
|
||||
return has, nil
|
||||
}
|
||||
has, err := global.DBConn.ID(uid).Exist(&model.User{})
|
||||
if has {
|
||||
cache.CacheExists(fmt.Sprintf("user_%s", uid), "user", uid)
|
||||
}
|
||||
return has, err
|
||||
}
|
||||
|
||||
func (_UserService) IsUsernameExists(username string) (bool, error) {
|
||||
return global.DBConn.Where(builder.Eq{"username": username}).Exist(&model.User{})
|
||||
if has, _ := cache.CheckExists("user", username); has {
|
||||
return has, nil
|
||||
}
|
||||
has, err := global.DBConn.Where(builder.Eq{"username": username}).Exist(&model.User{})
|
||||
if has {
|
||||
cache.CacheExists("user", "user", username)
|
||||
}
|
||||
return has, err
|
||||
}
|
||||
|
||||
func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) {
|
||||
@@ -205,6 +220,7 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st
|
||||
tx.Rollback()
|
||||
return "", fmt.Errorf("transaction commit unsuccessful: %w", err)
|
||||
}
|
||||
cache.AbolishRelation("user")
|
||||
return verifyCode, nil
|
||||
}
|
||||
|
||||
@@ -219,10 +235,17 @@ func (u _UserService) SwitchUserState(uid string, enabled bool) error {
|
||||
newStateUser := new(model.User)
|
||||
newStateUser.Enabled = enabled
|
||||
_, err = global.DBConn.ID(uid).Cols("enabled").Update(newStateUser)
|
||||
if err != nil {
|
||||
cache.AbolishRelation("user")
|
||||
cache.AbolishRelation(fmt.Sprintf("user_%s", uid))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedUserDetail, error) {
|
||||
if cachedUsers, _ := cache.RetreiveSearch[[]model.JoinedUserDetail]("join_user_detail", keyword, strconv.Itoa(limit)); cachedUsers != nil {
|
||||
return *cachedUsers, nil
|
||||
}
|
||||
var users = make([]model.JoinedUserDetail, 0)
|
||||
err := global.DBConn.
|
||||
Table("user_detail").Alias("d").
|
||||
@@ -239,83 +262,105 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU
|
||||
if err != nil {
|
||||
return make([]model.JoinedUserDetail, 0), err
|
||||
}
|
||||
cache.CacheSearch(users, "user", "join_user_detail", strconv.Itoa(limit))
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (_UserService) findUserByUsername(username string) (*model.User, error) {
|
||||
cachedUser, _ := cache.RetreiveData[model.User]("user", username)
|
||||
if cachedUser != nil {
|
||||
if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil {
|
||||
return cachedUser, nil
|
||||
}
|
||||
user := new(model.User)
|
||||
has, err := global.DBConn.Where(builder.Eq{"username": username}).NoAutoCondition().Get(user)
|
||||
if has {
|
||||
cache.CacheData(user, "user", username)
|
||||
cache.CacheSearch(*user, "user", "user", username)
|
||||
}
|
||||
return _postProcessSingle(user, has, err)
|
||||
}
|
||||
|
||||
func (_UserService) retreiveUserDetail(uid string) (*model.UserDetail, error) {
|
||||
cachedUser, _ := cache.RetreiveData[model.UserDetail]("user", uid)
|
||||
if cachedUser != nil {
|
||||
if cachedUser, _ := cache.RetreiveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil {
|
||||
return cachedUser, nil
|
||||
}
|
||||
user := new(model.UserDetail)
|
||||
has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user)
|
||||
if has {
|
||||
cache.CacheData(user, "user_detail", uid)
|
||||
cache.CacheEntity(*user, fmt.Sprintf("user_%s", uid), "user_detail", uid)
|
||||
}
|
||||
return _postProcessSingle(user, has, err)
|
||||
}
|
||||
|
||||
func (_UserService) findUserByID(uid string) (*model.User, error) {
|
||||
cachedUser, _ := cache.RetreiveData[model.User]("user", uid)
|
||||
cachedUser, _ := cache.RetreiveEntity[model.User]("user", uid)
|
||||
if cachedUser != nil {
|
||||
return cachedUser, nil
|
||||
}
|
||||
user := new(model.User)
|
||||
has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user)
|
||||
if has {
|
||||
cache.CacheData(user, "user", uid)
|
||||
cache.CacheEntity(*user, fmt.Sprintf("user_%s", uid), "user", uid)
|
||||
}
|
||||
return _postProcessSingle(user, has, err)
|
||||
}
|
||||
|
||||
func (_UserService) ListUserDetail(keyword string, userType int, userState *bool, page int) ([]model.JoinedUserDetail, int64, error) {
|
||||
var cond = builder.NewCond()
|
||||
var (
|
||||
cond = builder.NewCond()
|
||||
cacheConditions = make([]string, 0)
|
||||
)
|
||||
cacheConditions = append(cacheConditions, strconv.Itoa(page))
|
||||
cond = cond.And(builder.Neq{"d.id": "000"})
|
||||
if len(keyword) != 0 {
|
||||
keywordCond := builder.NewCond().
|
||||
Or(builder.Like{"u.username", keyword}).
|
||||
Or(builder.Like{"d.name", keyword})
|
||||
cond = cond.And(keywordCond)
|
||||
cacheConditions = append(cacheConditions, keyword)
|
||||
}
|
||||
if userType != -1 {
|
||||
cond = cond.And(builder.Eq{"u.type": userType})
|
||||
cacheConditions = append(cacheConditions, strconv.Itoa(userType))
|
||||
}
|
||||
if userState != nil {
|
||||
cond = cond.And(builder.Eq{"u.enabled": *userState})
|
||||
cacheConditions = append(cacheConditions, strconv.FormatBool(*userState))
|
||||
}
|
||||
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||
total, err := global.DBConn.
|
||||
Table("user_detail").Alias("d").
|
||||
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
||||
Where(cond).
|
||||
Count(&model.User{})
|
||||
if err != nil {
|
||||
return nil, -1, err
|
||||
var (
|
||||
total int64
|
||||
err error
|
||||
)
|
||||
if cacheCounts, _ := cache.RetreiveCount("join_user_detail", cacheConditions...); cacheCounts != -1 {
|
||||
total = cacheCounts
|
||||
} else {
|
||||
total, err = global.DBConn.
|
||||
Table("user_detail").Alias("d").
|
||||
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
||||
Where(cond).
|
||||
Count(&model.User{})
|
||||
if err != nil {
|
||||
return nil, -1, err
|
||||
}
|
||||
cache.CacheCount("user", "join_user_detail", total, cacheConditions...)
|
||||
}
|
||||
users := make([]model.JoinedUserDetail, 0)
|
||||
if cachedUsers, _ := cache.RetreiveSearch[[]model.JoinedUserDetail]("join_user_detail", cacheConditions...); cachedUsers != nil {
|
||||
return *cachedUsers, total, nil
|
||||
}
|
||||
err = global.DBConn.
|
||||
Table("user_detail").Alias("d").
|
||||
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
||||
Where(cond).
|
||||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
||||
Find(&users)
|
||||
cache.CacheSearch(users, "user", "join_user_detail", cacheConditions...)
|
||||
return users, total, err
|
||||
}
|
||||
|
||||
func (_UserService) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, error) {
|
||||
if cachedUser, _ := cache.RetreiveEntity[model.FullJoinedUserDetail]("full_join_user_detail", uid); cachedUser != nil {
|
||||
return cachedUser, nil
|
||||
}
|
||||
user := &model.FullJoinedUserDetail{}
|
||||
has, err := global.DBConn.
|
||||
Table("user_detail").Alias("d").
|
||||
@@ -324,6 +369,7 @@ func (_UserService) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, er
|
||||
NoAutoCondition().
|
||||
Get(user)
|
||||
if has {
|
||||
cache.CacheEntity(*user, fmt.Sprintf("user_%s", uid), "full_join_user_detail", uid)
|
||||
return user, nil
|
||||
}
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user