正在显示
10 个修改的文件
包含
326 行增加
和
161 行删除
@@ -9,7 +9,6 @@ var SERVICE_NAME = "allied-creation-user" | @@ -9,7 +9,6 @@ var SERVICE_NAME = "allied-creation-user" | ||
9 | var SERVICE_ENV = "dev" | 9 | var SERVICE_ENV = "dev" |
10 | var CACHE_PREFIX = "allied-creation-user-dev" | 10 | var CACHE_PREFIX = "allied-creation-user-dev" |
11 | var LOG_LEVEL = "debug" | 11 | var LOG_LEVEL = "debug" |
12 | -var EnableCaching = false | ||
13 | 12 | ||
14 | func init() { | 13 | func init() { |
15 | if os.Getenv("LOG_LEVEL") != "" { | 14 | if os.Getenv("LOG_LEVEL") != "" { |
@@ -6,7 +6,7 @@ var ( | @@ -6,7 +6,7 @@ var ( | ||
6 | REDIS_HOST = "127.0.0.1" | 6 | REDIS_HOST = "127.0.0.1" |
7 | REDIS_PORT = "6379" | 7 | REDIS_PORT = "6379" |
8 | REDIS_AUTH = "" | 8 | REDIS_AUTH = "" |
9 | - // 是否启用仓储层缓存 | 9 | + // 是否关闭仓储层缓存 |
10 | DISABLE_REPOSITORY_CACHE = false | 10 | DISABLE_REPOSITORY_CACHE = false |
11 | // 缓存过期时间 单位秒 | 11 | // 缓存过期时间 单位秒 |
12 | REPOSITORY_CACHE_EXPIRE = 30 * 60 | 12 | REPOSITORY_CACHE_EXPIRE = 30 * 60 |
@@ -2,6 +2,7 @@ package domain | @@ -2,6 +2,7 @@ package domain | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
5 | "time" | 6 | "time" |
6 | ) | 7 | ) |
7 | 8 | ||
@@ -174,3 +175,15 @@ func (org *Org) PID() string { | @@ -174,3 +175,15 @@ func (org *Org) PID() string { | ||
174 | func (org *Org) ID() string { | 175 | func (org *Org) ID() string { |
175 | return org.GetFullPath() | 176 | return org.GetFullPath() |
176 | } | 177 | } |
178 | + | ||
179 | +/***** 2.缓存模块 *****/ | ||
180 | + | ||
181 | +func (m *Org) CacheKeyFunc() string { | ||
182 | + if constant.DISABLE_REPOSITORY_CACHE { | ||
183 | + return "" | ||
184 | + } | ||
185 | + if m.OrgId == 0 { | ||
186 | + return "" | ||
187 | + } | ||
188 | + return fmt.Sprintf("%v:cache:org:id:%v", constant.CACHE_PREFIX, m.OrgId) | ||
189 | +} |
1 | package domain | 1 | package domain |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
4 | "time" | 6 | "time" |
5 | ) | 7 | ) |
6 | 8 | ||
@@ -116,3 +118,15 @@ func (role *Role) IsDeleted() bool { | @@ -116,3 +118,15 @@ func (role *Role) IsDeleted() bool { | ||
116 | } | 118 | } |
117 | return true | 119 | return true |
118 | } | 120 | } |
121 | + | ||
122 | +/***** 2.缓存模块 *****/ | ||
123 | + | ||
124 | +func (m *Role) CacheKeyFunc() string { | ||
125 | + if constant.DISABLE_REPOSITORY_CACHE { | ||
126 | + return "" | ||
127 | + } | ||
128 | + if m.RoleId == 0 { | ||
129 | + return "" | ||
130 | + } | ||
131 | + return fmt.Sprintf("%v:cache:role:id:%v", constant.CACHE_PREFIX, m.RoleId) | ||
132 | +} |
@@ -2,6 +2,7 @@ package domain | @@ -2,6 +2,7 @@ package domain | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
5 | "time" | 6 | "time" |
6 | ) | 7 | ) |
7 | 8 | ||
@@ -178,3 +179,15 @@ func (user *User) SetEnableStatus(enableStatus int) error { | @@ -178,3 +179,15 @@ func (user *User) SetEnableStatus(enableStatus int) error { | ||
178 | user.EnableStatus = enableStatus | 179 | user.EnableStatus = enableStatus |
179 | return nil | 180 | return nil |
180 | } | 181 | } |
182 | + | ||
183 | +/***** 2.缓存模块 *****/ | ||
184 | + | ||
185 | +func (m *User) CacheKeyFunc() string { | ||
186 | + if constant.DISABLE_REPOSITORY_CACHE { | ||
187 | + return "" | ||
188 | + } | ||
189 | + if m.UserId == 0 { | ||
190 | + return "" | ||
191 | + } | ||
192 | + return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, m.UserId) | ||
193 | +} |
@@ -2,6 +2,7 @@ package domain | @@ -2,6 +2,7 @@ package domain | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
5 | "strings" | 6 | "strings" |
6 | "time" | 7 | "time" |
7 | ) | 8 | ) |
@@ -176,3 +177,15 @@ func (userBase *UserBase) UpdateUserInfo(userInfo *UserInfo) error { | @@ -176,3 +177,15 @@ func (userBase *UserBase) UpdateUserInfo(userInfo *UserInfo) error { | ||
176 | userBase.UpdatedAt = time.Now() | 177 | userBase.UpdatedAt = time.Now() |
177 | return nil | 178 | return nil |
178 | } | 179 | } |
180 | + | ||
181 | +/***** 2.缓存模块 *****/ | ||
182 | + | ||
183 | +func (m *UserBase) CacheKeyFunc() string { | ||
184 | + if constant.DISABLE_REPOSITORY_CACHE { | ||
185 | + return "" | ||
186 | + } | ||
187 | + if m.UserBaseId == 0 { | ||
188 | + return "" | ||
189 | + } | ||
190 | + return fmt.Sprintf("%v:cache:userbase:id:%v", constant.CACHE_PREFIX, m.UserBaseId) | ||
191 | +} |
@@ -3,6 +3,8 @@ package repository | @@ -3,6 +3,8 @@ package repository | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "github.com/go-pg/pg/v10" | 5 | "github.com/go-pg/pg/v10" |
6 | + "github.com/linmadan/egglib-go/persistent/cache" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
6 | 8 | ||
7 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | 9 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" |
8 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 10 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
@@ -13,6 +15,7 @@ import ( | @@ -13,6 +15,7 @@ import ( | ||
13 | ) | 15 | ) |
14 | 16 | ||
15 | type OrgRepository struct { | 17 | type OrgRepository struct { |
18 | + *cache.CachedRepository | ||
16 | transactionContext *pgTransaction.TransactionContext | 19 | transactionContext *pgTransaction.TransactionContext |
17 | } | 20 | } |
18 | 21 | ||
@@ -84,35 +87,42 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) { | @@ -84,35 +87,42 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) { | ||
84 | return org, err | 87 | return org, err |
85 | } | 88 | } |
86 | } else { | 89 | } else { |
87 | - if _, err := tx.QueryOne( | ||
88 | - pg.Scan( | ||
89 | - &org.OrgId, | ||
90 | - &org.CompanyId, | ||
91 | - &org.CreatedAt, | ||
92 | - &org.UpdatedAt, | ||
93 | - &org.DeletedAt, | ||
94 | - &org.OrgCode, | ||
95 | - &org.OrgName, | ||
96 | - &org.Ext, | ||
97 | - &org.OrgStatus, | ||
98 | - &org.IsOrg, | ||
99 | - &org.ParentId, | ||
100 | - &org.ParentPath, | ||
101 | - ), | ||
102 | - fmt.Sprintf("UPDATE users.org SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
103 | - org.CompanyId, | ||
104 | - org.CreatedAt, | ||
105 | - org.UpdatedAt, | ||
106 | - org.DeletedAt, | ||
107 | - org.OrgCode, | ||
108 | - org.OrgName, | ||
109 | - org.Ext, | ||
110 | - org.OrgStatus, | ||
111 | - org.IsOrg, | ||
112 | - org.ParentId, | ||
113 | - org.ParentPath, | ||
114 | - org.Identify(), | ||
115 | - ); err != nil { | 90 | + queryFunc := func() (interface{}, error) { |
91 | + if _, err := tx.QueryOne( | ||
92 | + pg.Scan( | ||
93 | + &org.OrgId, | ||
94 | + &org.CompanyId, | ||
95 | + &org.CreatedAt, | ||
96 | + &org.UpdatedAt, | ||
97 | + &org.DeletedAt, | ||
98 | + &org.OrgCode, | ||
99 | + &org.OrgName, | ||
100 | + &org.Ext, | ||
101 | + &org.OrgStatus, | ||
102 | + &org.IsOrg, | ||
103 | + &org.ParentId, | ||
104 | + &org.ParentPath, | ||
105 | + ), | ||
106 | + fmt.Sprintf("UPDATE users.org SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
107 | + org.CompanyId, | ||
108 | + org.CreatedAt, | ||
109 | + org.UpdatedAt, | ||
110 | + org.DeletedAt, | ||
111 | + org.OrgCode, | ||
112 | + org.OrgName, | ||
113 | + org.Ext, | ||
114 | + org.OrgStatus, | ||
115 | + org.IsOrg, | ||
116 | + org.ParentId, | ||
117 | + org.ParentPath, | ||
118 | + org.Identify(), | ||
119 | + ); err != nil { | ||
120 | + return org, err | ||
121 | + } | ||
122 | + return org, nil | ||
123 | + } | ||
124 | + | ||
125 | + if _, err := repository.Query(queryFunc, org.CacheKeyFunc()); err != nil { | ||
116 | return org, err | 126 | return org, err |
117 | } | 127 | } |
118 | } | 128 | } |
@@ -122,27 +132,48 @@ func (repository *OrgRepository) Remove(org *domain.Org) (*domain.Org, error) { | @@ -122,27 +132,48 @@ func (repository *OrgRepository) Remove(org *domain.Org) (*domain.Org, error) { | ||
122 | tx := repository.transactionContext.PgTx | 132 | tx := repository.transactionContext.PgTx |
123 | orgModel := new(models.Org) | 133 | orgModel := new(models.Org) |
124 | orgModel.OrgId = org.Identify().(int64) | 134 | orgModel.OrgId = org.Identify().(int64) |
125 | - if _, err := tx.Model(orgModel).WherePK().Delete(); err != nil { | 135 | + queryFunc := func() (interface{}, error) { |
136 | + if _, err := tx.Model(orgModel).WherePK().Delete(); err != nil { | ||
137 | + return org, err | ||
138 | + } | ||
139 | + return org, nil | ||
140 | + } | ||
141 | + | ||
142 | + if _, err := repository.Query(queryFunc, org.CacheKeyFunc()); err != nil { | ||
126 | return org, err | 143 | return org, err |
127 | } | 144 | } |
145 | + | ||
128 | return org, nil | 146 | return org, nil |
129 | } | 147 | } |
130 | func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (*domain.Org, error) { | 148 | func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (*domain.Org, error) { |
131 | tx := repository.transactionContext.PgTx | 149 | tx := repository.transactionContext.PgTx |
132 | orgModel := new(models.Org) | 150 | orgModel := new(models.Org) |
133 | - query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions) | ||
134 | - query.SetWhereByQueryOption("company_id = ?", "companyId") | ||
135 | - query.SetWhereByQueryOption("org_id = ?", "orgId") | ||
136 | - query.SetWhereByQueryOption("org_name = ?", "orgName") | ||
137 | - query.SetWhereByQueryOption("org_code = ?", "orgCode") | ||
138 | - query.SetWhereByQueryOption("org_id != ?", "notEqualOrgId") | ||
139 | - if err := query.First(); err != nil { | ||
140 | - if err.Error() == "pg: no rows in result set" { | ||
141 | - return nil, fmt.Errorf("没有此资源") | ||
142 | - } else { | ||
143 | - return nil, err | 151 | + |
152 | + queryFunc := func() (interface{}, error) { | ||
153 | + query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions) | ||
154 | + query.SetWhereByQueryOption("company_id = ?", "companyId") | ||
155 | + query.SetWhereByQueryOption("org_id = ?", "orgId") | ||
156 | + query.SetWhereByQueryOption("org_name = ?", "orgName") | ||
157 | + query.SetWhereByQueryOption("org_code = ?", "orgCode") | ||
158 | + query.SetWhereByQueryOption("org_id != ?", "notEqualOrgId") | ||
159 | + if err := query.First(); err != nil { | ||
160 | + if err.Error() == "pg: no rows in result set" { | ||
161 | + return nil, fmt.Errorf("没有此资源") | ||
162 | + } else { | ||
163 | + return nil, err | ||
164 | + } | ||
144 | } | 165 | } |
166 | + return orgModel, nil | ||
167 | + } | ||
168 | + | ||
169 | + var cacheModel = &domain.Org{} | ||
170 | + if _, ok := queryOptions["orgId"]; ok { | ||
171 | + cacheModel.OrgId = queryOptions["orgId"].(int64) | ||
145 | } | 172 | } |
173 | + if err := repository.QueryCache(cacheModel.CacheKeyFunc, orgModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil { | ||
174 | + return nil, err | ||
175 | + } | ||
176 | + | ||
146 | if orgModel.OrgId == 0 { | 177 | if orgModel.OrgId == 0 { |
147 | return nil, nil | 178 | return nil, nil |
148 | } else { | 179 | } else { |
@@ -178,6 +209,7 @@ func NewOrgRepository(transactionContext *pgTransaction.TransactionContext) (*Or | @@ -178,6 +209,7 @@ func NewOrgRepository(transactionContext *pgTransaction.TransactionContext) (*Or | ||
178 | } else { | 209 | } else { |
179 | return &OrgRepository{ | 210 | return &OrgRepository{ |
180 | transactionContext: transactionContext, | 211 | transactionContext: transactionContext, |
212 | + CachedRepository: cache.NewDefaultCachedRepository(), | ||
181 | }, nil | 213 | }, nil |
182 | } | 214 | } |
183 | } | 215 | } |
@@ -3,6 +3,8 @@ package repository | @@ -3,6 +3,8 @@ package repository | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "github.com/go-pg/pg/v10" | 5 | "github.com/go-pg/pg/v10" |
6 | + "github.com/linmadan/egglib-go/persistent/cache" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
6 | 8 | ||
7 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | 9 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" |
8 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 10 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
@@ -13,6 +15,7 @@ import ( | @@ -13,6 +15,7 @@ import ( | ||
13 | ) | 15 | ) |
14 | 16 | ||
15 | type RoleRepository struct { | 17 | type RoleRepository struct { |
18 | + *cache.CachedRepository | ||
16 | transactionContext *pgTransaction.TransactionContext | 19 | transactionContext *pgTransaction.TransactionContext |
17 | } | 20 | } |
18 | 21 | ||
@@ -78,31 +81,37 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error) | @@ -78,31 +81,37 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error) | ||
78 | return role, err | 81 | return role, err |
79 | } | 82 | } |
80 | } else { | 83 | } else { |
81 | - if _, err := tx.QueryOne( | ||
82 | - pg.Scan( | ||
83 | - &role.RoleId, | ||
84 | - &role.CompanyId, | ||
85 | - &role.OrgId, | ||
86 | - &role.RoleType, | ||
87 | - &role.RoleName, | ||
88 | - pg.Array(&role.AccessMenus), | ||
89 | - &role.Desc, | ||
90 | - &role.Ext, | ||
91 | - &role.CreatedAt, | ||
92 | - &role.UpdatedAt, | ||
93 | - ), | ||
94 | - fmt.Sprintf("UPDATE users.role SET %s WHERE role_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
95 | - role.CompanyId, | ||
96 | - role.OrgId, | ||
97 | - role.RoleType, | ||
98 | - role.RoleName, | ||
99 | - pg.Array(role.AccessMenus), | ||
100 | - role.Desc, | ||
101 | - role.Ext, | ||
102 | - role.CreatedAt, | ||
103 | - role.UpdatedAt, | ||
104 | - role.Identify(), | ||
105 | - ); err != nil { | 84 | + queryFunc := func() (interface{}, error) { |
85 | + if _, err := tx.QueryOne( | ||
86 | + pg.Scan( | ||
87 | + &role.RoleId, | ||
88 | + &role.CompanyId, | ||
89 | + &role.OrgId, | ||
90 | + &role.RoleType, | ||
91 | + &role.RoleName, | ||
92 | + pg.Array(&role.AccessMenus), | ||
93 | + &role.Desc, | ||
94 | + &role.Ext, | ||
95 | + &role.CreatedAt, | ||
96 | + &role.UpdatedAt, | ||
97 | + ), | ||
98 | + fmt.Sprintf("UPDATE users.role SET %s WHERE role_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
99 | + role.CompanyId, | ||
100 | + role.OrgId, | ||
101 | + role.RoleType, | ||
102 | + role.RoleName, | ||
103 | + pg.Array(role.AccessMenus), | ||
104 | + role.Desc, | ||
105 | + role.Ext, | ||
106 | + role.CreatedAt, | ||
107 | + role.UpdatedAt, | ||
108 | + role.Identify(), | ||
109 | + ); err != nil { | ||
110 | + return role, err | ||
111 | + } | ||
112 | + return role, nil | ||
113 | + } | ||
114 | + if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil { | ||
106 | return role, err | 115 | return role, err |
107 | } | 116 | } |
108 | } | 117 | } |
@@ -112,7 +121,13 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error | @@ -112,7 +121,13 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error | ||
112 | tx := repository.transactionContext.PgTx | 121 | tx := repository.transactionContext.PgTx |
113 | roleModel := new(models.Role) | 122 | roleModel := new(models.Role) |
114 | roleModel.RoleId = role.Identify().(int64) | 123 | roleModel.RoleId = role.Identify().(int64) |
115 | - if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil { | 124 | + queryFunc := func() (interface{}, error) { |
125 | + if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil { | ||
126 | + return role, err | ||
127 | + } | ||
128 | + return role, nil | ||
129 | + } | ||
130 | + if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil { | ||
116 | return role, err | 131 | return role, err |
117 | } | 132 | } |
118 | return role, nil | 133 | return role, nil |
@@ -120,22 +135,34 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error | @@ -120,22 +135,34 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error | ||
120 | func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) { | 135 | func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) { |
121 | tx := repository.transactionContext.PgTx | 136 | tx := repository.transactionContext.PgTx |
122 | roleModel := new(models.Role) | 137 | roleModel := new(models.Role) |
123 | - query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions) | ||
124 | - query.SetWhereByQueryOption("role_id = ?", "roleId") | ||
125 | - query.SetWhereByQueryOption("company_id = ?", "companyId") | ||
126 | - query.SetWhereByQueryOption("org_id = ?", "orgId") | ||
127 | - query.SetWhereByQueryOption("role_name = ?", "roleName") | ||
128 | - query.SetWhereByQueryOption("(role_type & ?) >0", "roleType") | ||
129 | - if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) { | ||
130 | - query.AllWithDeleted() | ||
131 | - } | ||
132 | - if err := query.First(); err != nil { | ||
133 | - if err.Error() == "pg: no rows in result set" { | ||
134 | - return nil, fmt.Errorf("没有此资源") | ||
135 | - } else { | ||
136 | - return nil, err | 138 | + queryFunc := func() (interface{}, error) { |
139 | + query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions) | ||
140 | + query.SetWhereByQueryOption("role_id = ?", "roleId") | ||
141 | + query.SetWhereByQueryOption("company_id = ?", "companyId") | ||
142 | + query.SetWhereByQueryOption("org_id = ?", "orgId") | ||
143 | + query.SetWhereByQueryOption("role_name = ?", "roleName") | ||
144 | + query.SetWhereByQueryOption("(role_type & ?) >0", "roleType") | ||
145 | + if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) { | ||
146 | + query.AllWithDeleted() | ||
147 | + } | ||
148 | + if err := query.First(); err != nil { | ||
149 | + if err.Error() == "pg: no rows in result set" { | ||
150 | + return nil, fmt.Errorf("没有此资源") | ||
151 | + } else { | ||
152 | + return nil, err | ||
153 | + } | ||
137 | } | 154 | } |
155 | + return roleModel, nil | ||
156 | + } | ||
157 | + | ||
158 | + var cacheModel = &domain.Role{} | ||
159 | + if _, ok := queryOptions["roleId"]; ok { | ||
160 | + cacheModel.RoleId = queryOptions["roleId"].(int64) | ||
138 | } | 161 | } |
162 | + if err := repository.QueryCache(cacheModel.CacheKeyFunc, roleModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil { | ||
163 | + return nil, err | ||
164 | + } | ||
165 | + | ||
139 | if roleModel.RoleId == 0 { | 166 | if roleModel.RoleId == 0 { |
140 | return nil, nil | 167 | return nil, nil |
141 | } else { | 168 | } else { |
@@ -176,6 +203,7 @@ func NewRoleRepository(transactionContext *pgTransaction.TransactionContext) (*R | @@ -176,6 +203,7 @@ func NewRoleRepository(transactionContext *pgTransaction.TransactionContext) (*R | ||
176 | } else { | 203 | } else { |
177 | return &RoleRepository{ | 204 | return &RoleRepository{ |
178 | transactionContext: transactionContext, | 205 | transactionContext: transactionContext, |
206 | + CachedRepository: cache.NewDefaultCachedRepository(), | ||
179 | }, nil | 207 | }, nil |
180 | } | 208 | } |
181 | } | 209 | } |
@@ -3,6 +3,8 @@ package repository | @@ -3,6 +3,8 @@ package repository | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "github.com/go-pg/pg/v10" | 5 | "github.com/go-pg/pg/v10" |
6 | + "github.com/linmadan/egglib-go/persistent/cache" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
6 | 8 | ||
7 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | 9 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" |
8 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 10 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
@@ -13,6 +15,7 @@ import ( | @@ -13,6 +15,7 @@ import ( | ||
13 | ) | 15 | ) |
14 | 16 | ||
15 | type UserBaseRepository struct { | 17 | type UserBaseRepository struct { |
18 | + *cache.CachedRepository | ||
16 | transactionContext *pgTransaction.TransactionContext | 19 | transactionContext *pgTransaction.TransactionContext |
17 | } | 20 | } |
18 | 21 | ||
@@ -80,29 +83,35 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U | @@ -80,29 +83,35 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U | ||
80 | return userBase, err | 83 | return userBase, err |
81 | } | 84 | } |
82 | } else { | 85 | } else { |
83 | - if _, err := tx.QueryOne( | ||
84 | - pg.Scan( | ||
85 | - &userBase.UserBaseId, | ||
86 | - &userBase.UserInfo, | ||
87 | - &userBase.Account, | ||
88 | - &userBase.Password, | ||
89 | - &userBase.Im, | ||
90 | - pg.Array(&userBase.RelatedUsers), | ||
91 | - &userBase.Status, | ||
92 | - &userBase.CreatedAt, | ||
93 | - &userBase.UpdatedAt, | ||
94 | - ), | ||
95 | - fmt.Sprintf("UPDATE users.user_base SET %s WHERE user_base_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
96 | - userBase.UserInfo, | ||
97 | - userBase.Account, | ||
98 | - userBase.Password, | ||
99 | - userBase.Im, | ||
100 | - pg.Array(userBase.RelatedUsers), | ||
101 | - userBase.Status, | ||
102 | - userBase.CreatedAt, | ||
103 | - userBase.UpdatedAt, | ||
104 | - userBase.Identify(), | ||
105 | - ); err != nil { | 86 | + queryFunc := func() (interface{}, error) { |
87 | + if _, err := tx.QueryOne( | ||
88 | + pg.Scan( | ||
89 | + &userBase.UserBaseId, | ||
90 | + &userBase.UserInfo, | ||
91 | + &userBase.Account, | ||
92 | + &userBase.Password, | ||
93 | + &userBase.Im, | ||
94 | + pg.Array(&userBase.RelatedUsers), | ||
95 | + &userBase.Status, | ||
96 | + &userBase.CreatedAt, | ||
97 | + &userBase.UpdatedAt, | ||
98 | + ), | ||
99 | + fmt.Sprintf("UPDATE users.user_base SET %s WHERE user_base_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
100 | + userBase.UserInfo, | ||
101 | + userBase.Account, | ||
102 | + userBase.Password, | ||
103 | + userBase.Im, | ||
104 | + pg.Array(userBase.RelatedUsers), | ||
105 | + userBase.Status, | ||
106 | + userBase.CreatedAt, | ||
107 | + userBase.UpdatedAt, | ||
108 | + userBase.Identify(), | ||
109 | + ); err != nil { | ||
110 | + return userBase, err | ||
111 | + } | ||
112 | + return userBase, nil | ||
113 | + } | ||
114 | + if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil { | ||
106 | return userBase, err | 115 | return userBase, err |
107 | } | 116 | } |
108 | } | 117 | } |
@@ -112,7 +121,13 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain | @@ -112,7 +121,13 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain | ||
112 | tx := repository.transactionContext.PgTx | 121 | tx := repository.transactionContext.PgTx |
113 | userBaseModel := new(models.UserBase) | 122 | userBaseModel := new(models.UserBase) |
114 | userBaseModel.UserBaseId = userBase.Identify().(int64) | 123 | userBaseModel.UserBaseId = userBase.Identify().(int64) |
115 | - if _, err := tx.Model(userBaseModel).WherePK().Delete(); err != nil { | 124 | + queryFunc := func() (interface{}, error) { |
125 | + if _, err := tx.Model(userBaseModel).WherePK().Delete(); err != nil { | ||
126 | + return userBase, err | ||
127 | + } | ||
128 | + return userBase, nil | ||
129 | + } | ||
130 | + if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil { | ||
116 | return userBase, err | 131 | return userBase, err |
117 | } | 132 | } |
118 | return userBase, nil | 133 | return userBase, nil |
@@ -120,15 +135,26 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain | @@ -120,15 +135,26 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain | ||
120 | func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.UserBase, error) { | 135 | func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.UserBase, error) { |
121 | tx := repository.transactionContext.PgTx | 136 | tx := repository.transactionContext.PgTx |
122 | userBaseModel := new(models.UserBase) | 137 | userBaseModel := new(models.UserBase) |
123 | - query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions) | ||
124 | - query.SetWhereByQueryOption("account = ?", "account") | ||
125 | - query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId") | ||
126 | - if err := query.First(); err != nil { | ||
127 | - if err.Error() == "pg: no rows in result set" { | ||
128 | - return nil, domain.ErrorNotFound | ||
129 | - } else { | ||
130 | - return nil, err | 138 | + queryFunc := func() (interface{}, error) { |
139 | + query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions) | ||
140 | + query.SetWhereByQueryOption("account = ?", "account") | ||
141 | + query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId") | ||
142 | + if err := query.First(); err != nil { | ||
143 | + if err.Error() == "pg: no rows in result set" { | ||
144 | + return nil, domain.ErrorNotFound | ||
145 | + } else { | ||
146 | + return nil, err | ||
147 | + } | ||
131 | } | 148 | } |
149 | + return userBaseModel, nil | ||
150 | + } | ||
151 | + | ||
152 | + var cacheModel = &domain.UserBase{} | ||
153 | + if _, ok := queryOptions["userBaseId"]; ok { | ||
154 | + cacheModel.UserBaseId = queryOptions["userBaseId"].(int64) | ||
155 | + } | ||
156 | + if err := repository.QueryCache(cacheModel.CacheKeyFunc, userBaseModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil { | ||
157 | + return nil, err | ||
132 | } | 158 | } |
133 | if userBaseModel.UserBaseId == 0 { | 159 | if userBaseModel.UserBaseId == 0 { |
134 | return nil, nil | 160 | return nil, nil |
@@ -162,6 +188,7 @@ func NewUserBaseRepository(transactionContext *pgTransaction.TransactionContext) | @@ -162,6 +188,7 @@ func NewUserBaseRepository(transactionContext *pgTransaction.TransactionContext) | ||
162 | } else { | 188 | } else { |
163 | return &UserBaseRepository{ | 189 | return &UserBaseRepository{ |
164 | transactionContext: transactionContext, | 190 | transactionContext: transactionContext, |
191 | + CachedRepository: cache.NewDefaultCachedRepository(), | ||
165 | }, nil | 192 | }, nil |
166 | } | 193 | } |
167 | } | 194 | } |
@@ -3,6 +3,8 @@ package repository | @@ -3,6 +3,8 @@ package repository | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "github.com/go-pg/pg/v10" | 5 | "github.com/go-pg/pg/v10" |
6 | + "github.com/linmadan/egglib-go/persistent/cache" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant" | ||
6 | 8 | ||
7 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | 9 | "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" |
8 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 10 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
@@ -13,6 +15,7 @@ import ( | @@ -13,6 +15,7 @@ import ( | ||
13 | ) | 15 | ) |
14 | 16 | ||
15 | type UserRepository struct { | 17 | type UserRepository struct { |
18 | + *cache.CachedRepository | ||
16 | transactionContext *pgTransaction.TransactionContext | 19 | transactionContext *pgTransaction.TransactionContext |
17 | } | 20 | } |
18 | 21 | ||
@@ -93,41 +96,47 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | @@ -93,41 +96,47 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) | ||
93 | return user, err | 96 | return user, err |
94 | } | 97 | } |
95 | } else { | 98 | } else { |
96 | - if _, err := tx.QueryOne( | ||
97 | - pg.Scan( | ||
98 | - &user.UserId, | ||
99 | - &user.CompanyId, | ||
100 | - &user.UserBaseId, | ||
101 | - &user.UserType, | ||
102 | - &user.UserCode, | ||
103 | - &user.OrganizationId, | ||
104 | - &user.DepartmentId, | ||
105 | - &user.UserOrg, | ||
106 | - &user.UserRole, | ||
107 | - &user.FavoriteMenus, | ||
108 | - &user.CooperationInfo, | ||
109 | - &user.EnableStatus, | ||
110 | - &user.Ext, | ||
111 | - &user.CreatedAt, | ||
112 | - &user.UpdatedAt, | ||
113 | - ), | ||
114 | - fmt.Sprintf(`UPDATE users."user" SET %s WHERE user_id=? RETURNING %s`, updateFieldsSnippet, returningFieldsSnippet), | ||
115 | - user.CompanyId, | ||
116 | - user.UserBaseId, | ||
117 | - user.UserType, | ||
118 | - user.UserCode, | ||
119 | - user.OrganizationId, | ||
120 | - user.DepartmentId, | ||
121 | - user.UserOrg, | ||
122 | - user.UserRole, | ||
123 | - user.FavoriteMenus, | ||
124 | - user.CooperationInfo, | ||
125 | - user.EnableStatus, | ||
126 | - user.Ext, | ||
127 | - user.CreatedAt, | ||
128 | - user.UpdatedAt, | ||
129 | - user.Identify(), | ||
130 | - ); err != nil { | 99 | + queryFunc := func() (interface{}, error) { |
100 | + if _, err := tx.QueryOne( | ||
101 | + pg.Scan( | ||
102 | + &user.UserId, | ||
103 | + &user.CompanyId, | ||
104 | + &user.UserBaseId, | ||
105 | + &user.UserType, | ||
106 | + &user.UserCode, | ||
107 | + &user.OrganizationId, | ||
108 | + &user.DepartmentId, | ||
109 | + &user.UserOrg, | ||
110 | + &user.UserRole, | ||
111 | + &user.FavoriteMenus, | ||
112 | + &user.CooperationInfo, | ||
113 | + &user.EnableStatus, | ||
114 | + &user.Ext, | ||
115 | + &user.CreatedAt, | ||
116 | + &user.UpdatedAt, | ||
117 | + ), | ||
118 | + fmt.Sprintf(`UPDATE users."user" SET %s WHERE user_id=? RETURNING %s`, updateFieldsSnippet, returningFieldsSnippet), | ||
119 | + user.CompanyId, | ||
120 | + user.UserBaseId, | ||
121 | + user.UserType, | ||
122 | + user.UserCode, | ||
123 | + user.OrganizationId, | ||
124 | + user.DepartmentId, | ||
125 | + user.UserOrg, | ||
126 | + user.UserRole, | ||
127 | + user.FavoriteMenus, | ||
128 | + user.CooperationInfo, | ||
129 | + user.EnableStatus, | ||
130 | + user.Ext, | ||
131 | + user.CreatedAt, | ||
132 | + user.UpdatedAt, | ||
133 | + user.Identify(), | ||
134 | + ); err != nil { | ||
135 | + return user, err | ||
136 | + } | ||
137 | + return user, nil | ||
138 | + } | ||
139 | + if _, err := repository.Query(queryFunc, user.CacheKeyFunc()); err != nil { | ||
131 | return user, err | 140 | return user, err |
132 | } | 141 | } |
133 | } | 142 | } |
@@ -137,7 +146,13 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error | @@ -137,7 +146,13 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error | ||
137 | tx := repository.transactionContext.PgTx | 146 | tx := repository.transactionContext.PgTx |
138 | userModel := new(models.User) | 147 | userModel := new(models.User) |
139 | userModel.UserId = user.Identify().(int64) | 148 | userModel.UserId = user.Identify().(int64) |
140 | - if _, err := tx.Model(userModel).WherePK().Delete(); err != nil { | 149 | + queryFunc := func() (interface{}, error) { |
150 | + if _, err := tx.Model(userModel).WherePK().Delete(); err != nil { | ||
151 | + return user, err | ||
152 | + } | ||
153 | + return user, nil | ||
154 | + } | ||
155 | + if _, err := repository.Query(queryFunc, user.CacheKeyFunc()); err != nil { | ||
141 | return user, err | 156 | return user, err |
142 | } | 157 | } |
143 | return user, nil | 158 | return user, nil |
@@ -145,17 +160,27 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error | @@ -145,17 +160,27 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error | ||
145 | func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) { | 160 | func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) { |
146 | tx := repository.transactionContext.PgTx | 161 | tx := repository.transactionContext.PgTx |
147 | userModel := new(models.User) | 162 | userModel := new(models.User) |
148 | - query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions) | ||
149 | - query.SetWhereByQueryOption("user_id = ?", "userId") | ||
150 | - query.SetWhereByQueryOption("company_id=?", "companyId") | ||
151 | - query.SetWhereByQueryOption("organization_id=?", "organizationId") | ||
152 | - query.SetWhereByQueryOption("user_code = ?", "userCode") | ||
153 | - if err := query.First(); err != nil { | ||
154 | - if err.Error() == "pg: no rows in result set" { | ||
155 | - return nil, fmt.Errorf("没有此资源") | ||
156 | - } else { | ||
157 | - return nil, err | 163 | + queryFunc := func() (interface{}, error) { |
164 | + query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions) | ||
165 | + query.SetWhereByQueryOption("user_id = ?", "userId") | ||
166 | + query.SetWhereByQueryOption("company_id=?", "companyId") | ||
167 | + query.SetWhereByQueryOption("organization_id=?", "organizationId") | ||
168 | + query.SetWhereByQueryOption("user_code = ?", "userCode") | ||
169 | + if err := query.First(); err != nil { | ||
170 | + if err.Error() == "pg: no rows in result set" { | ||
171 | + return nil, fmt.Errorf("没有此资源") | ||
172 | + } else { | ||
173 | + return nil, err | ||
174 | + } | ||
158 | } | 175 | } |
176 | + return userModel, nil | ||
177 | + } | ||
178 | + var cacheModel = &domain.User{} | ||
179 | + if _, ok := queryOptions["userId"]; ok { | ||
180 | + cacheModel.UserId = queryOptions["userId"].(int64) | ||
181 | + } | ||
182 | + if err := repository.QueryCache(cacheModel.CacheKeyFunc, userModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil { | ||
183 | + return nil, err | ||
159 | } | 184 | } |
160 | if userModel.UserId == 0 { | 185 | if userModel.UserId == 0 { |
161 | return nil, nil | 186 | return nil, nil |
@@ -201,6 +226,7 @@ func NewUserRepository(transactionContext *pgTransaction.TransactionContext) (*U | @@ -201,6 +226,7 @@ func NewUserRepository(transactionContext *pgTransaction.TransactionContext) (*U | ||
201 | } else { | 226 | } else { |
202 | return &UserRepository{ | 227 | return &UserRepository{ |
203 | transactionContext: transactionContext, | 228 | transactionContext: transactionContext, |
229 | + CachedRepository: cache.NewDefaultCachedRepository(), | ||
204 | }, nil | 230 | }, nil |
205 | } | 231 | } |
206 | } | 232 | } |
-
请 注册 或 登录 后发表评论