作者 yangfu

添加缓存支持

... ... @@ -9,7 +9,6 @@ var SERVICE_NAME = "allied-creation-user"
var SERVICE_ENV = "dev"
var CACHE_PREFIX = "allied-creation-user-dev"
var LOG_LEVEL = "debug"
var EnableCaching = false
func init() {
if os.Getenv("LOG_LEVEL") != "" {
... ...
... ... @@ -6,7 +6,7 @@ var (
REDIS_HOST = "127.0.0.1"
REDIS_PORT = "6379"
REDIS_AUTH = ""
// 是否启用仓储层缓存
// 是否关闭仓储层缓存
DISABLE_REPOSITORY_CACHE = false
// 缓存过期时间 单位秒
REPOSITORY_CACHE_EXPIRE = 30 * 60
... ...
... ... @@ -2,6 +2,7 @@ package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"time"
)
... ... @@ -174,3 +175,15 @@ func (org *Org) PID() string {
func (org *Org) ID() string {
return org.GetFullPath()
}
/***** 2.缓存模块 *****/
func (m *Org) CacheKeyFunc() string {
if constant.DISABLE_REPOSITORY_CACHE {
return ""
}
if m.OrgId == 0 {
return ""
}
return fmt.Sprintf("%v:cache:org:id:%v", constant.CACHE_PREFIX, m.OrgId)
}
... ...
package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"time"
)
... ... @@ -116,3 +118,15 @@ func (role *Role) IsDeleted() bool {
}
return true
}
/***** 2.缓存模块 *****/
func (m *Role) CacheKeyFunc() string {
if constant.DISABLE_REPOSITORY_CACHE {
return ""
}
if m.RoleId == 0 {
return ""
}
return fmt.Sprintf("%v:cache:role:id:%v", constant.CACHE_PREFIX, m.RoleId)
}
... ...
... ... @@ -2,6 +2,7 @@ package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"time"
)
... ... @@ -178,3 +179,15 @@ func (user *User) SetEnableStatus(enableStatus int) error {
user.EnableStatus = enableStatus
return nil
}
/***** 2.缓存模块 *****/
func (m *User) CacheKeyFunc() string {
if constant.DISABLE_REPOSITORY_CACHE {
return ""
}
if m.UserId == 0 {
return ""
}
return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, m.UserId)
}
... ...
... ... @@ -2,6 +2,7 @@ package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"strings"
"time"
)
... ... @@ -176,3 +177,15 @@ func (userBase *UserBase) UpdateUserInfo(userInfo *UserInfo) error {
userBase.UpdatedAt = time.Now()
return nil
}
/***** 2.缓存模块 *****/
func (m *UserBase) CacheKeyFunc() string {
if constant.DISABLE_REPOSITORY_CACHE {
return ""
}
if m.UserBaseId == 0 {
return ""
}
return fmt.Sprintf("%v:cache:userbase:id:%v", constant.CACHE_PREFIX, m.UserBaseId)
}
... ...
... ... @@ -3,6 +3,8 @@ package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/cache"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -13,6 +15,7 @@ import (
)
type OrgRepository struct {
*cache.CachedRepository
transactionContext *pgTransaction.TransactionContext
}
... ... @@ -84,6 +87,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
return org, err
}
} else {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&org.OrgId,
... ... @@ -115,6 +119,12 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
); err != nil {
return org, err
}
return org, nil
}
if _, err := repository.Query(queryFunc, org.CacheKeyFunc()); err != nil {
return org, err
}
}
return org, nil
}
... ... @@ -122,14 +132,24 @@ func (repository *OrgRepository) Remove(org *domain.Org) (*domain.Org, error) {
tx := repository.transactionContext.PgTx
orgModel := new(models.Org)
orgModel.OrgId = org.Identify().(int64)
queryFunc := func() (interface{}, error) {
if _, err := tx.Model(orgModel).WherePK().Delete(); err != nil {
return org, err
}
return org, nil
}
if _, err := repository.Query(queryFunc, org.CacheKeyFunc()); err != nil {
return org, err
}
return org, nil
}
func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (*domain.Org, error) {
tx := repository.transactionContext.PgTx
orgModel := new(models.Org)
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions)
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
... ... @@ -143,6 +163,17 @@ func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (*
return nil, err
}
}
return orgModel, nil
}
var cacheModel = &domain.Org{}
if _, ok := queryOptions["orgId"]; ok {
cacheModel.OrgId = queryOptions["orgId"].(int64)
}
if err := repository.QueryCache(cacheModel.CacheKeyFunc, orgModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
return nil, err
}
if orgModel.OrgId == 0 {
return nil, nil
} else {
... ... @@ -178,6 +209,7 @@ func NewOrgRepository(transactionContext *pgTransaction.TransactionContext) (*Or
} else {
return &OrgRepository{
transactionContext: transactionContext,
CachedRepository: cache.NewDefaultCachedRepository(),
}, nil
}
}
... ...
... ... @@ -3,6 +3,8 @@ package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/cache"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -13,6 +15,7 @@ import (
)
type RoleRepository struct {
*cache.CachedRepository
transactionContext *pgTransaction.TransactionContext
}
... ... @@ -78,6 +81,7 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error)
return role, err
}
} else {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&role.RoleId,
... ... @@ -105,6 +109,11 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error)
); err != nil {
return role, err
}
return role, nil
}
if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil {
return role, err
}
}
return role, nil
}
... ... @@ -112,14 +121,21 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error
tx := repository.transactionContext.PgTx
roleModel := new(models.Role)
roleModel.RoleId = role.Identify().(int64)
queryFunc := func() (interface{}, error) {
if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil {
return role, err
}
return role, nil
}
if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil {
return role, err
}
return role, nil
}
func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
tx := repository.transactionContext.PgTx
roleModel := new(models.Role)
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions)
query.SetWhereByQueryOption("role_id = ?", "roleId")
query.SetWhereByQueryOption("company_id = ?", "companyId")
... ... @@ -136,6 +152,17 @@ func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (
return nil, err
}
}
return roleModel, nil
}
var cacheModel = &domain.Role{}
if _, ok := queryOptions["roleId"]; ok {
cacheModel.RoleId = queryOptions["roleId"].(int64)
}
if err := repository.QueryCache(cacheModel.CacheKeyFunc, roleModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
return nil, err
}
if roleModel.RoleId == 0 {
return nil, nil
} else {
... ... @@ -176,6 +203,7 @@ func NewRoleRepository(transactionContext *pgTransaction.TransactionContext) (*R
} else {
return &RoleRepository{
transactionContext: transactionContext,
CachedRepository: cache.NewDefaultCachedRepository(),
}, nil
}
}
... ...
... ... @@ -3,6 +3,8 @@ package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/cache"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -13,6 +15,7 @@ import (
)
type UserBaseRepository struct {
*cache.CachedRepository
transactionContext *pgTransaction.TransactionContext
}
... ... @@ -80,6 +83,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
return userBase, err
}
} else {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&userBase.UserBaseId,
... ... @@ -105,6 +109,11 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
); err != nil {
return userBase, err
}
return userBase, nil
}
if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil {
return userBase, err
}
}
return userBase, nil
}
... ... @@ -112,14 +121,21 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain
tx := repository.transactionContext.PgTx
userBaseModel := new(models.UserBase)
userBaseModel.UserBaseId = userBase.Identify().(int64)
queryFunc := func() (interface{}, error) {
if _, err := tx.Model(userBaseModel).WherePK().Delete(); err != nil {
return userBase, err
}
return userBase, nil
}
if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil {
return userBase, err
}
return userBase, nil
}
func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.UserBase, error) {
tx := repository.transactionContext.PgTx
userBaseModel := new(models.UserBase)
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions)
query.SetWhereByQueryOption("account = ?", "account")
query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId")
... ... @@ -130,6 +146,16 @@ func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{
return nil, err
}
}
return userBaseModel, nil
}
var cacheModel = &domain.UserBase{}
if _, ok := queryOptions["userBaseId"]; ok {
cacheModel.UserBaseId = queryOptions["userBaseId"].(int64)
}
if err := repository.QueryCache(cacheModel.CacheKeyFunc, userBaseModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
return nil, err
}
if userBaseModel.UserBaseId == 0 {
return nil, nil
} else {
... ... @@ -162,6 +188,7 @@ func NewUserBaseRepository(transactionContext *pgTransaction.TransactionContext)
} else {
return &UserBaseRepository{
transactionContext: transactionContext,
CachedRepository: cache.NewDefaultCachedRepository(),
}, nil
}
}
... ...
... ... @@ -3,6 +3,8 @@ package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/cache"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -13,6 +15,7 @@ import (
)
type UserRepository struct {
*cache.CachedRepository
transactionContext *pgTransaction.TransactionContext
}
... ... @@ -93,6 +96,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
return user, err
}
} else {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&user.UserId,
... ... @@ -130,6 +134,11 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
); err != nil {
return user, err
}
return user, nil
}
if _, err := repository.Query(queryFunc, user.CacheKeyFunc()); err != nil {
return user, err
}
}
return user, nil
}
... ... @@ -137,14 +146,21 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error
tx := repository.transactionContext.PgTx
userModel := new(models.User)
userModel.UserId = user.Identify().(int64)
queryFunc := func() (interface{}, error) {
if _, err := tx.Model(userModel).WherePK().Delete(); err != nil {
return user, err
}
return user, nil
}
if _, err := repository.Query(queryFunc, user.CacheKeyFunc()); err != nil {
return user, err
}
return user, nil
}
func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) {
tx := repository.transactionContext.PgTx
userModel := new(models.User)
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions)
query.SetWhereByQueryOption("user_id = ?", "userId")
query.SetWhereByQueryOption("company_id=?", "companyId")
... ... @@ -157,6 +173,15 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (
return nil, err
}
}
return userModel, nil
}
var cacheModel = &domain.User{}
if _, ok := queryOptions["userId"]; ok {
cacheModel.UserId = queryOptions["userId"].(int64)
}
if err := repository.QueryCache(cacheModel.CacheKeyFunc, userModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
return nil, err
}
if userModel.UserId == 0 {
return nil, nil
} else {
... ... @@ -201,6 +226,7 @@ func NewUserRepository(transactionContext *pgTransaction.TransactionContext) (*U
} else {
return &UserRepository{
transactionContext: transactionContext,
CachedRepository: cache.NewDefaultCachedRepository(),
}, nil
}
}
... ...