作者 yangfu

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

@@ -98,6 +98,7 @@ func (svr *RoleService) GetRole(header *protocol.RequestHeader, request *protoco @@ -98,6 +98,7 @@ func (svr *RoleService) GetRole(header *protocol.RequestHeader, request *protoco
98 var ( 98 var (
99 transactionContext, _ = factory.CreateTransactionContext(nil) 99 transactionContext, _ = factory.CreateTransactionContext(nil)
100 ) 100 )
  101 + transactionContext.SetTransactionClose()
101 rsp = &protocolx.GetRoleResponse{} 102 rsp = &protocolx.GetRoleResponse{}
102 if err = request.ValidateCommand(); err != nil { 103 if err = request.ValidateCommand(); err != nil {
103 err = protocol.NewCustomMessage(2, err.Error()) 104 err = protocol.NewCustomMessage(2, err.Error())
@@ -3,11 +3,15 @@ package transaction @@ -3,11 +3,15 @@ package transaction
3 import "github.com/go-pg/pg/v10" 3 import "github.com/go-pg/pg/v10"
4 4
5 type TransactionContext struct { 5 type TransactionContext struct {
6 - PgDd *pg.DB  
7 - PgTx *pg.Tx 6 + CloseTransactionFlag bool //事务关闭标识
  7 + PgDd *pg.DB
  8 + PgTx *pg.Tx
8 } 9 }
9 10
10 func (transactionContext *TransactionContext) StartTransaction() error { 11 func (transactionContext *TransactionContext) StartTransaction() error {
  12 + if transactionContext.CloseTransactionFlag {
  13 + return nil
  14 + }
11 tx, err := transactionContext.PgDd.Begin() 15 tx, err := transactionContext.PgDd.Begin()
12 if err != nil { 16 if err != nil {
13 return err 17 return err
@@ -17,15 +21,29 @@ func (transactionContext *TransactionContext) StartTransaction() error { @@ -17,15 +21,29 @@ func (transactionContext *TransactionContext) StartTransaction() error {
17 } 21 }
18 22
19 func (transactionContext *TransactionContext) CommitTransaction() error { 23 func (transactionContext *TransactionContext) CommitTransaction() error {
  24 + if transactionContext.CloseTransactionFlag {
  25 + return nil
  26 + }
20 err := transactionContext.PgTx.Commit() 27 err := transactionContext.PgTx.Commit()
21 return err 28 return err
22 } 29 }
23 30
24 func (transactionContext *TransactionContext) RollbackTransaction() error { 31 func (transactionContext *TransactionContext) RollbackTransaction() error {
  32 + if transactionContext.CloseTransactionFlag {
  33 + return nil
  34 + }
25 err := transactionContext.PgTx.Rollback() 35 err := transactionContext.PgTx.Rollback()
26 return err 36 return err
27 } 37 }
28 38
  39 +// SetTransactionClose
  40 +// 在不需要事务的地方可以执行该方法关闭事务处理
  41 +// 例如:对象的查询
  42 +func (transactionContext *TransactionContext) SetTransactionClose() error {
  43 + transactionContext.CloseTransactionFlag = true
  44 + return nil
  45 +}
  46 +
29 func NewPGTransactionContext(pgDd *pg.DB) *TransactionContext { 47 func NewPGTransactionContext(pgDd *pg.DB) *TransactionContext {
30 return &TransactionContext{ 48 return &TransactionContext{
31 PgDd: pgDd, 49 PgDd: pgDd,
@@ -4,13 +4,21 @@ import ( @@ -4,13 +4,21 @@ import (
4 "fmt" 4 "fmt"
5 "github.com/go-pg/pg/v10" 5 "github.com/go-pg/pg/v10"
6 "github.com/tiptok/gocomm/common" 6 "github.com/tiptok/gocomm/common"
  7 + "github.com/tiptok/gocomm/pkg/cache"
7 . "github.com/tiptok/gocomm/pkg/orm/pgx" 8 . "github.com/tiptok/gocomm/pkg/orm/pgx"
8 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain" 9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
9 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models" 10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
10 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction" 11 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
11 ) 12 )
12 13
  14 +var (
  15 + cacheRoleIdKey = func(id int64) string {
  16 + return fmt.Sprintf("%v:cache:Role:id:%v", "godevp", id)
  17 + }
  18 +)
  19 +
13 type RoleRepository struct { 20 type RoleRepository struct {
  21 + *cache.CachedRepository
14 transactionContext *transaction.TransactionContext 22 transactionContext *transaction.TransactionContext
15 } 23 }
16 24
@@ -30,7 +38,10 @@ func (repository *RoleRepository) Save(dm *domain.Role) (*domain.Role, error) { @@ -30,7 +38,10 @@ func (repository *RoleRepository) Save(dm *domain.Role) (*domain.Role, error) {
30 dm.Id = m.Id 38 dm.Id = m.Id
31 return dm, nil 39 return dm, nil
32 } 40 }
33 - if _, err = tx.Exec(`update role set role_name=?,parent_id=?,update_time=now() where id = ?`, m.RoleName, m.ParentId, m.Id); err != nil { 41 + queryFunc := func() (interface{}, error) {
  42 + return tx.Exec(`update role set role_name=?,parent_id=?,update_time=now() where id = ?`, m.RoleName, m.ParentId, m.Id)
  43 + }
  44 + if _, err = repository.Query(queryFunc, cacheRoleIdKey(dm.Id)); err != nil {
34 return nil, err 45 return nil, err
35 } 46 }
36 return dm, nil 47 return dm, nil
@@ -41,20 +52,42 @@ func (repository *RoleRepository) Remove(Role *domain.Role) (*domain.Role, error @@ -41,20 +52,42 @@ func (repository *RoleRepository) Remove(Role *domain.Role) (*domain.Role, error
41 tx = repository.transactionContext.PgTx 52 tx = repository.transactionContext.PgTx
42 RoleModel = &models.Role{Id: Role.Identify().(int64)} 53 RoleModel = &models.Role{Id: Role.Identify().(int64)}
43 ) 54 )
44 - if _, err := tx.Model(RoleModel).Where("id = ?", Role.Id).Delete(); err != nil { 55 + queryFunc := func() (interface{}, error) {
  56 + return tx.Model(RoleModel).Where("id = ?", Role.Id).Delete()
  57 + }
  58 + if _, err := repository.Query(queryFunc, cacheRoleIdKey(Role.Id)); err != nil {
45 return Role, err 59 return Role, err
46 } 60 }
  61 + //if _, err := tx.Model(RoleModel).Where("id = ?", Role.Id).Delete(); err != nil {
  62 + // return Role, err
  63 + //}
47 return Role, nil 64 return Role, nil
48 } 65 }
49 66
50 func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) { 67 func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
51 - tx := repository.transactionContext.PgTx 68 + tx := repository.transactionContext.PgDd
52 RoleModel := new(models.Role) 69 RoleModel := new(models.Role)
53 - query := NewQuery(tx.Model(RoleModel), queryOptions)  
54 - query.SetWhere("id = ?", "id")  
55 - if err := query.First(); err != nil {  
56 - return nil, fmt.Errorf("query row not found") 70 + //query := NewQuery(tx.Model(RoleModel), queryOptions)
  71 + //query.SetWhere("id = ?", "id")
  72 + //if err := query.First(); err != nil {
  73 + // return nil, fmt.Errorf("query row not found")
  74 + //}
  75 + queryFunc := func() (interface{}, error) {
  76 + query := NewQuery(tx.Model(RoleModel), queryOptions)
  77 + query.SetWhere("id = ?", "id")
  78 + if err := query.First(); err != nil {
  79 + return nil, fmt.Errorf("query row not found")
  80 + }
  81 + return RoleModel, nil
  82 + }
  83 + var options []cache.QueryOption
  84 + if _, ok := queryOptions["id"]; !ok {
  85 + options = append(options, cache.WithNoCacheFlag())
57 } 86 }
  87 + if err := repository.QueryCache(cacheRoleIdKey(queryOptions["id"].(int64)), RoleModel, queryFunc, options...); err != nil {
  88 + return nil, err
  89 + }
  90 +
58 if RoleModel.Id == 0 { 91 if RoleModel.Id == 0 {
59 return nil, fmt.Errorf("query row not found") 92 return nil, fmt.Errorf("query row not found")
60 } 93 }
@@ -98,5 +131,5 @@ func NewRoleRepository(transactionContext *transaction.TransactionContext) (*Rol @@ -98,5 +131,5 @@ func NewRoleRepository(transactionContext *transaction.TransactionContext) (*Rol
98 if transactionContext == nil { 131 if transactionContext == nil {
99 return nil, fmt.Errorf("transactionContext参数不能为nil") 132 return nil, fmt.Errorf("transactionContext参数不能为nil")
100 } 133 }
101 - return &RoleRepository{transactionContext: transactionContext}, nil 134 + return &RoleRepository{transactionContext: transactionContext, CachedRepository: cache.NewDefaultCachedRepository()}, nil
102 } 135 }
@@ -26,12 +26,6 @@ func LoadCustomField(src interface{}, fields ...string) interface{} { @@ -26,12 +26,6 @@ func LoadCustomField(src interface{}, fields ...string) interface{} {
26 } 26 }
27 for i := 0; i < len; i++ { 27 for i := 0; i < len; i++ {
28 v := valueSrc.Index(i) 28 v := valueSrc.Index(i)
29 - //if v.Kind()==reflect.Ptr  
30 - //item :=make(map[string]interface{})  
31 - //for _,filed :=range fields{  
32 - // f:= v.FieldByName(filed)  
33 - // item[common.CamelCase(filed, false)] = f.Interface()  
34 - //}  
35 retSliceMap = append(retSliceMap, (LoadCustomField(v, fields...)).(map[string]interface{})) 29 retSliceMap = append(retSliceMap, (LoadCustomField(v, fields...)).(map[string]interface{}))
36 } 30 }
37 return retSliceMap 31 return retSliceMap
  1 +package auth
  2 +
  3 +import "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
  4 +
  5 +type RoleDto struct {
  6 + // 唯一标识
  7 + Id int64 `json:"id"`
  8 + // 角色名称
  9 + RoleName string `json:"roleName"`
  10 + // 父级Id
  11 + ParentId int64 `json:"-"`
  12 +}
  13 +
  14 +func (dto *RoleDto) LoadDto(role *domain.Role) error {
  15 + dto.Id = role.Id
  16 + dto.RoleName = role.RoleName
  17 + dto.ParentId = role.ParentId
  18 + return nil
  19 +}
  20 +
  21 +func NewRoleDtoList(role []*domain.Role) []*RoleDto {
  22 + var retRoleDto []*RoleDto
  23 + for _, v := range role {
  24 + roleDto := new(RoleDto)
  25 + roleDto.LoadDto(v)
  26 + retRoleDto = append(retRoleDto, roleDto)
  27 + }
  28 + return retRoleDto
  29 +}