作者 yangfu

修改: 1.角色仓储增加缓存

... ... @@ -98,6 +98,7 @@ func (svr *RoleService) GetRole(header *protocol.RequestHeader, request *protoco
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
transactionContext.SetTransactionClose()
rsp = &protocolx.GetRoleResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
... ...
... ... @@ -3,11 +3,15 @@ package transaction
import "github.com/go-pg/pg/v10"
type TransactionContext struct {
CloseTransactionFlag bool //事务关闭标识
PgDd *pg.DB
PgTx *pg.Tx
}
func (transactionContext *TransactionContext) StartTransaction() error {
if transactionContext.CloseTransactionFlag {
return nil
}
tx, err := transactionContext.PgDd.Begin()
if err != nil {
return err
... ... @@ -17,15 +21,29 @@ func (transactionContext *TransactionContext) StartTransaction() error {
}
func (transactionContext *TransactionContext) CommitTransaction() error {
if transactionContext.CloseTransactionFlag {
return nil
}
err := transactionContext.PgTx.Commit()
return err
}
func (transactionContext *TransactionContext) RollbackTransaction() error {
if transactionContext.CloseTransactionFlag {
return nil
}
err := transactionContext.PgTx.Rollback()
return err
}
// SetTransactionClose
// 在不需要事务的地方可以执行该方法关闭事务处理
// 例如:对象的查询
func (transactionContext *TransactionContext) SetTransactionClose() error {
transactionContext.CloseTransactionFlag = true
return nil
}
func NewPGTransactionContext(pgDd *pg.DB) *TransactionContext {
return &TransactionContext{
PgDd: pgDd,
... ...
... ... @@ -4,13 +4,21 @@ import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/cache"
. "github.com/tiptok/gocomm/pkg/orm/pgx"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)
var (
cacheRoleIdKey = func(id int64) string {
return fmt.Sprintf("%v:cache:Role:id:%v", "godevp", id)
}
)
type RoleRepository struct {
*cache.CachedRepository
transactionContext *transaction.TransactionContext
}
... ... @@ -30,7 +38,10 @@ func (repository *RoleRepository) Save(dm *domain.Role) (*domain.Role, error) {
dm.Id = m.Id
return dm, nil
}
if _, err = tx.Exec(`update role set role_name=?,parent_id=?,update_time=now() where id = ?`, m.RoleName, m.ParentId, m.Id); err != nil {
queryFunc := func() (interface{}, error) {
return tx.Exec(`update role set role_name=?,parent_id=?,update_time=now() where id = ?`, m.RoleName, m.ParentId, m.Id)
}
if _, err = repository.Query(queryFunc, cacheRoleIdKey(dm.Id)); err != nil {
return nil, err
}
return dm, nil
... ... @@ -41,20 +52,42 @@ func (repository *RoleRepository) Remove(Role *domain.Role) (*domain.Role, error
tx = repository.transactionContext.PgTx
RoleModel = &models.Role{Id: Role.Identify().(int64)}
)
if _, err := tx.Model(RoleModel).Where("id = ?", Role.Id).Delete(); err != nil {
queryFunc := func() (interface{}, error) {
return tx.Model(RoleModel).Where("id = ?", Role.Id).Delete()
}
if _, err := repository.Query(queryFunc, cacheRoleIdKey(Role.Id)); err != nil {
return Role, err
}
//if _, err := tx.Model(RoleModel).Where("id = ?", Role.Id).Delete(); err != nil {
// return Role, err
//}
return Role, nil
}
func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
tx := repository.transactionContext.PgTx
tx := repository.transactionContext.PgDd
RoleModel := new(models.Role)
//query := NewQuery(tx.Model(RoleModel), queryOptions)
//query.SetWhere("id = ?", "id")
//if err := query.First(); err != nil {
// return nil, fmt.Errorf("query row not found")
//}
queryFunc := func() (interface{}, error) {
query := NewQuery(tx.Model(RoleModel), queryOptions)
query.SetWhere("id = ?", "id")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
return RoleModel, nil
}
var options []cache.QueryOption
if _, ok := queryOptions["id"]; !ok {
options = append(options, cache.WithNoCacheFlag())
}
if err := repository.QueryCache(cacheRoleIdKey(queryOptions["id"].(int64)), RoleModel, queryFunc, options...); err != nil {
return nil, err
}
if RoleModel.Id == 0 {
return nil, fmt.Errorf("query row not found")
}
... ... @@ -98,5 +131,5 @@ func NewRoleRepository(transactionContext *transaction.TransactionContext) (*Rol
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &RoleRepository{transactionContext: transactionContext}, nil
return &RoleRepository{transactionContext: transactionContext, CachedRepository: cache.NewDefaultCachedRepository()}, nil
}
... ...
... ... @@ -26,12 +26,6 @@ func LoadCustomField(src interface{}, fields ...string) interface{} {
}
for i := 0; i < len; i++ {
v := valueSrc.Index(i)
//if v.Kind()==reflect.Ptr
//item :=make(map[string]interface{})
//for _,filed :=range fields{
// f:= v.FieldByName(filed)
// item[common.CamelCase(filed, false)] = f.Interface()
//}
retSliceMap = append(retSliceMap, (LoadCustomField(v, fields...)).(map[string]interface{}))
}
return retSliceMap
... ...
package auth
import "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
type RoleDto struct {
// 唯一标识
Id int64 `json:"id"`
// 角色名称
RoleName string `json:"roleName"`
// 父级Id
ParentId int64 `json:"-"`
}
func (dto *RoleDto) LoadDto(role *domain.Role) error {
dto.Id = role.Id
dto.RoleName = role.RoleName
dto.ParentId = role.ParentId
return nil
}
func NewRoleDtoList(role []*domain.Role) []*RoleDto {
var retRoleDto []*RoleDto
for _, v := range role {
roleDto := new(RoleDto)
roleDto.LoadDto(v)
retRoleDto = append(retRoleDto, roleDto)
}
return retRoleDto
}
... ...