2 Star 1 Fork 2

联犀/core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cache.go 4.14 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-11-05 00:18 . bug fix
package sysExport
import (
"context"
"fmt"
"gitee.com/unitedrhino/core/service/syssvr/client/areamanage"
"gitee.com/unitedrhino/core/service/syssvr/client/common"
"gitee.com/unitedrhino/core/service/syssvr/client/projectmanage"
"gitee.com/unitedrhino/core/service/syssvr/client/tenantmanage"
"gitee.com/unitedrhino/core/service/syssvr/client/usermanage"
"gitee.com/unitedrhino/core/service/syssvr/internal/logic"
"gitee.com/unitedrhino/core/service/syssvr/pb/sys"
"gitee.com/unitedrhino/share/caches"
"gitee.com/unitedrhino/share/ctxs"
"gitee.com/unitedrhino/share/domain/slot"
"gitee.com/unitedrhino/share/domain/tenant"
"gitee.com/unitedrhino/share/eventBus"
"gitee.com/unitedrhino/share/utils"
"strings"
)
type AreaCacheT = *caches.Cache[areamanage.AreaInfo, int64]
func NewAreaInfoCache(pm areamanage.AreaManage, fastEvent *eventBus.FastEvent) (AreaCacheT, error) {
return caches.NewCache(caches.CacheConfig[areamanage.AreaInfo, int64]{
KeyType: eventBus.ServerCacheKeySysAreaInfo,
FastEvent: fastEvent,
GetData: func(ctx context.Context, key int64) (*areamanage.AreaInfo, error) {
ret, err := pm.AreaInfoRead(ctx, &sys.AreaInfoReadReq{ProjectID: ctxs.GetUserCtxNoNil(ctx).ProjectID, AreaID: key})
return ret, err
},
})
}
type ProjectCacheT = *caches.Cache[projectmanage.ProjectInfo, int64]
func NewProjectInfoCache(pm projectmanage.ProjectManage, fastEvent *eventBus.FastEvent) (ProjectCacheT, error) {
return caches.NewCache(caches.CacheConfig[projectmanage.ProjectInfo, int64]{
KeyType: eventBus.ServerCacheKeySysProjectInfo,
FastEvent: fastEvent,
GetData: func(ctx context.Context, key int64) (*projectmanage.ProjectInfo, error) {
ret, err := pm.ProjectInfoRead(ctx, &sys.ProjectWithID{ProjectID: key})
return ret, err
},
})
}
type UserCacheT = *caches.Cache[usermanage.UserInfo, int64]
func NewUserInfoCache(pm usermanage.UserManage, fastEvent *eventBus.FastEvent) (UserCacheT, error) {
return caches.NewCache(caches.CacheConfig[usermanage.UserInfo, int64]{
KeyType: eventBus.ServerCacheKeySysUserInfo,
FastEvent: fastEvent,
GetData: func(ctx context.Context, key int64) (*usermanage.UserInfo, error) {
ret, err := pm.UserInfoRead(ctx, &sys.UserInfoReadReq{UserID: key})
return ret, err
},
})
}
type TenantCacheT = *caches.Cache[tenant.Info, string]
func NewTenantInfoCache(pm tenantmanage.TenantManage, fastEvent *eventBus.FastEvent) (TenantCacheT, error) {
return caches.NewCache(caches.CacheConfig[tenant.Info, string]{
KeyType: eventBus.ServerCacheKeySysTenantInfo,
FastEvent: fastEvent,
GetData: func(ctx context.Context, key string) (*tenant.Info, error) {
ret, err := pm.TenantInfoRead(ctx, &sys.WithIDCode{Code: key})
return logic.RpcToTenantInfoCache(ret), err
},
})
}
type WebHookCacheT = *caches.Cache[sys.TenantOpenWebHook, string]
func NewTenantOpenWebhookCache(pm tenantmanage.TenantManage, fastEvent *eventBus.FastEvent) (WebHookCacheT, error) {
return caches.NewCache(caches.CacheConfig[sys.TenantOpenWebHook, string]{
KeyType: eventBus.ServerCacheKeySysTenantOpenWebhook,
FastEvent: fastEvent,
GetData: func(ctx context.Context, key string) (*sys.TenantOpenWebHook, error) {
t := strings.Split(key, ":")
ret, err := pm.TenantOpenWebHook(ctx, &sys.WithCode{Code: t[1]})
return ret, err
},
})
}
type SlotCacheT = *caches.Cache[slot.Infos, string]
func NewSlotCache(pm common.Common) (SlotCacheT, error) {
return caches.NewCache(caches.CacheConfig[slot.Infos, string]{
KeyType: "slot",
GetData: func(ctx context.Context, key string) (*slot.Infos, error) {
t := strings.Split(key, ":")
ret, err := pm.SlotInfoIndex(ctx, &sys.SlotInfoIndexReq{Code: t[0], SubCode: t[1]})
if err != nil {
return nil, err
}
slots := slot.Infos(utils.CopySlice[slot.Info](ret.List))
return &slots, nil
},
})
}
func GenSlotCacheKey(code string, subCode string) string {
return fmt.Sprintf("%s:%s", code, subCode)
}
func GenWebhookCacheKey(ctx context.Context, code string) string {
tenantCode := ctxs.GetUserCtxNoNil(ctx).TenantCode
return fmt.Sprintf("%s:%s", tenantCode, code)
}
func GenApiCacheKey(method, route string) string {
return fmt.Sprintf("%s:%s", method, route)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/unitedrhino/core.git
git@gitee.com:unitedrhino/core.git
unitedrhino
core
core
v1.1.0

搜索帮助