|
@@ -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
|
} |