作者 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,35 +87,42 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
return org, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&org.OrgId,
&org.CompanyId,
&org.CreatedAt,
&org.UpdatedAt,
&org.DeletedAt,
&org.OrgCode,
&org.OrgName,
&org.Ext,
&org.OrgStatus,
&org.IsOrg,
&org.ParentId,
&org.ParentPath,
),
fmt.Sprintf("UPDATE users.org SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
org.CompanyId,
org.CreatedAt,
org.UpdatedAt,
org.DeletedAt,
org.OrgCode,
org.OrgName,
org.Ext,
org.OrgStatus,
org.IsOrg,
org.ParentId,
org.ParentPath,
org.Identify(),
); err != nil {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&org.OrgId,
&org.CompanyId,
&org.CreatedAt,
&org.UpdatedAt,
&org.DeletedAt,
&org.OrgCode,
&org.OrgName,
&org.Ext,
&org.OrgStatus,
&org.IsOrg,
&org.ParentId,
&org.ParentPath,
),
fmt.Sprintf("UPDATE users.org SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
org.CompanyId,
org.CreatedAt,
org.UpdatedAt,
org.DeletedAt,
org.OrgCode,
org.OrgName,
org.Ext,
org.OrgStatus,
org.IsOrg,
org.ParentId,
org.ParentPath,
org.Identify(),
); err != nil {
return org, err
}
return org, nil
}
if _, err := repository.Query(queryFunc, org.CacheKeyFunc()); err != nil {
return org, err
}
}
... ... @@ -122,27 +132,48 @@ func (repository *OrgRepository) Remove(org *domain.Org) (*domain.Org, error) {
tx := repository.transactionContext.PgTx
orgModel := new(models.Org)
orgModel.OrgId = org.Identify().(int64)
if _, err := tx.Model(orgModel).WherePK().Delete(); err != nil {
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)
query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions)
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("org_name = ?", "orgName")
query.SetWhereByQueryOption("org_code = ?", "orgCode")
query.SetWhereByQueryOption("org_id != ?", "notEqualOrgId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions)
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("org_name = ?", "orgName")
query.SetWhereByQueryOption("org_code = ?", "orgCode")
query.SetWhereByQueryOption("org_id != ?", "notEqualOrgId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
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,31 +81,37 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error)
return role, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&role.RoleId,
&role.CompanyId,
&role.OrgId,
&role.RoleType,
&role.RoleName,
pg.Array(&role.AccessMenus),
&role.Desc,
&role.Ext,
&role.CreatedAt,
&role.UpdatedAt,
),
fmt.Sprintf("UPDATE users.role SET %s WHERE role_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
role.CompanyId,
role.OrgId,
role.RoleType,
role.RoleName,
pg.Array(role.AccessMenus),
role.Desc,
role.Ext,
role.CreatedAt,
role.UpdatedAt,
role.Identify(),
); err != nil {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&role.RoleId,
&role.CompanyId,
&role.OrgId,
&role.RoleType,
&role.RoleName,
pg.Array(&role.AccessMenus),
&role.Desc,
&role.Ext,
&role.CreatedAt,
&role.UpdatedAt,
),
fmt.Sprintf("UPDATE users.role SET %s WHERE role_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
role.CompanyId,
role.OrgId,
role.RoleType,
role.RoleName,
pg.Array(role.AccessMenus),
role.Desc,
role.Ext,
role.CreatedAt,
role.UpdatedAt,
role.Identify(),
); err != nil {
return role, err
}
return role, nil
}
if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil {
return role, err
}
}
... ... @@ -112,7 +121,13 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error
tx := repository.transactionContext.PgTx
roleModel := new(models.Role)
roleModel.RoleId = role.Identify().(int64)
if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil {
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
... ... @@ -120,22 +135,34 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error
func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
tx := repository.transactionContext.PgTx
roleModel := new(models.Role)
query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions)
query.SetWhereByQueryOption("role_id = ?", "roleId")
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("role_name = ?", "roleName")
query.SetWhereByQueryOption("(role_type & ?) >0", "roleType")
if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
query.AllWithDeleted()
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions)
query.SetWhereByQueryOption("role_id = ?", "roleId")
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("role_name = ?", "roleName")
query.SetWhereByQueryOption("(role_type & ?) >0", "roleType")
if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
query.AllWithDeleted()
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
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,29 +83,35 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
return userBase, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&userBase.UserBaseId,
&userBase.UserInfo,
&userBase.Account,
&userBase.Password,
&userBase.Im,
pg.Array(&userBase.RelatedUsers),
&userBase.Status,
&userBase.CreatedAt,
&userBase.UpdatedAt,
),
fmt.Sprintf("UPDATE users.user_base SET %s WHERE user_base_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
userBase.UserInfo,
userBase.Account,
userBase.Password,
userBase.Im,
pg.Array(userBase.RelatedUsers),
userBase.Status,
userBase.CreatedAt,
userBase.UpdatedAt,
userBase.Identify(),
); err != nil {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&userBase.UserBaseId,
&userBase.UserInfo,
&userBase.Account,
&userBase.Password,
&userBase.Im,
pg.Array(&userBase.RelatedUsers),
&userBase.Status,
&userBase.CreatedAt,
&userBase.UpdatedAt,
),
fmt.Sprintf("UPDATE users.user_base SET %s WHERE user_base_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
userBase.UserInfo,
userBase.Account,
userBase.Password,
userBase.Im,
pg.Array(userBase.RelatedUsers),
userBase.Status,
userBase.CreatedAt,
userBase.UpdatedAt,
userBase.Identify(),
); err != nil {
return userBase, err
}
return userBase, nil
}
if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil {
return userBase, err
}
}
... ... @@ -112,7 +121,13 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain
tx := repository.transactionContext.PgTx
userBaseModel := new(models.UserBase)
userBaseModel.UserBaseId = userBase.Identify().(int64)
if _, err := tx.Model(userBaseModel).WherePK().Delete(); err != nil {
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
... ... @@ -120,15 +135,26 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain
func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.UserBase, error) {
tx := repository.transactionContext.PgTx
userBaseModel := new(models.UserBase)
query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions)
query.SetWhereByQueryOption("account = ?", "account")
query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
} else {
return nil, err
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions)
query.SetWhereByQueryOption("account = ?", "account")
query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
} else {
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
... ... @@ -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,41 +96,47 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
return user, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&user.UserId,
&user.CompanyId,
&user.UserBaseId,
&user.UserType,
&user.UserCode,
&user.OrganizationId,
&user.DepartmentId,
&user.UserOrg,
&user.UserRole,
&user.FavoriteMenus,
&user.CooperationInfo,
&user.EnableStatus,
&user.Ext,
&user.CreatedAt,
&user.UpdatedAt,
),
fmt.Sprintf(`UPDATE users."user" SET %s WHERE user_id=? RETURNING %s`, updateFieldsSnippet, returningFieldsSnippet),
user.CompanyId,
user.UserBaseId,
user.UserType,
user.UserCode,
user.OrganizationId,
user.DepartmentId,
user.UserOrg,
user.UserRole,
user.FavoriteMenus,
user.CooperationInfo,
user.EnableStatus,
user.Ext,
user.CreatedAt,
user.UpdatedAt,
user.Identify(),
); err != nil {
queryFunc := func() (interface{}, error) {
if _, err := tx.QueryOne(
pg.Scan(
&user.UserId,
&user.CompanyId,
&user.UserBaseId,
&user.UserType,
&user.UserCode,
&user.OrganizationId,
&user.DepartmentId,
&user.UserOrg,
&user.UserRole,
&user.FavoriteMenus,
&user.CooperationInfo,
&user.EnableStatus,
&user.Ext,
&user.CreatedAt,
&user.UpdatedAt,
),
fmt.Sprintf(`UPDATE users."user" SET %s WHERE user_id=? RETURNING %s`, updateFieldsSnippet, returningFieldsSnippet),
user.CompanyId,
user.UserBaseId,
user.UserType,
user.UserCode,
user.OrganizationId,
user.DepartmentId,
user.UserOrg,
user.UserRole,
user.FavoriteMenus,
user.CooperationInfo,
user.EnableStatus,
user.Ext,
user.CreatedAt,
user.UpdatedAt,
user.Identify(),
); err != nil {
return user, err
}
return user, nil
}
if _, err := repository.Query(queryFunc, user.CacheKeyFunc()); err != nil {
return user, err
}
}
... ... @@ -137,7 +146,13 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error
tx := repository.transactionContext.PgTx
userModel := new(models.User)
userModel.UserId = user.Identify().(int64)
if _, err := tx.Model(userModel).WherePK().Delete(); err != nil {
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
... ... @@ -145,17 +160,27 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error
func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) {
tx := repository.transactionContext.PgTx
userModel := new(models.User)
query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions)
query.SetWhereByQueryOption("user_id = ?", "userId")
query.SetWhereByQueryOption("company_id=?", "companyId")
query.SetWhereByQueryOption("organization_id=?", "organizationId")
query.SetWhereByQueryOption("user_code = ?", "userCode")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
queryFunc := func() (interface{}, error) {
query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions)
query.SetWhereByQueryOption("user_id = ?", "userId")
query.SetWhereByQueryOption("company_id=?", "companyId")
query.SetWhereByQueryOption("organization_id=?", "organizationId")
query.SetWhereByQueryOption("user_code = ?", "userCode")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
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
... ... @@ -201,6 +226,7 @@ func NewUserRepository(transactionContext *pgTransaction.TransactionContext) (*U
} else {
return &UserRepository{
transactionContext: transactionContext,
CachedRepository: cache.NewDefaultCachedRepository(),
}, nil
}
}
... ...