作者 yangfu

添加缓存支持

@@ -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,6 +87,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) { @@ -84,6 +87,7 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
84 return org, err 87 return org, err
85 } 88 }
86 } else { 89 } else {
  90 + queryFunc := func() (interface{}, error) {
87 if _, err := tx.QueryOne( 91 if _, err := tx.QueryOne(
88 pg.Scan( 92 pg.Scan(
89 &org.OrgId, 93 &org.OrgId,
@@ -115,6 +119,12 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) { @@ -115,6 +119,12 @@ func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
115 ); err != nil { 119 ); err != nil {
116 return org, err 120 return org, err
117 } 121 }
  122 + return org, nil
  123 + }
  124 +
  125 + if _, err := repository.Query(queryFunc, org.CacheKeyFunc()); err != nil {
  126 + return org, err
  127 + }
118 } 128 }
119 return org, nil 129 return org, nil
120 } 130 }
@@ -122,14 +132,24 @@ func (repository *OrgRepository) Remove(org *domain.Org) (*domain.Org, error) { @@ -122,14 +132,24 @@ 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)
  135 + queryFunc := func() (interface{}, error) {
125 if _, err := tx.Model(orgModel).WherePK().Delete(); err != nil { 136 if _, err := tx.Model(orgModel).WherePK().Delete(); err != nil {
126 return org, err 137 return org, err
127 } 138 }
128 return org, nil 139 return org, nil
  140 + }
  141 +
  142 + if _, err := repository.Query(queryFunc, org.CacheKeyFunc()); err != nil {
  143 + return org, err
  144 + }
  145 +
  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)
  151 +
  152 + queryFunc := func() (interface{}, error) {
133 query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions) 153 query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions)
134 query.SetWhereByQueryOption("company_id = ?", "companyId") 154 query.SetWhereByQueryOption("company_id = ?", "companyId")
135 query.SetWhereByQueryOption("org_id = ?", "orgId") 155 query.SetWhereByQueryOption("org_id = ?", "orgId")
@@ -143,6 +163,17 @@ func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (* @@ -143,6 +163,17 @@ func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (*
143 return nil, err 163 return nil, err
144 } 164 }
145 } 165 }
  166 + return orgModel, nil
  167 + }
  168 +
  169 + var cacheModel = &domain.Org{}
  170 + if _, ok := queryOptions["orgId"]; ok {
  171 + cacheModel.OrgId = queryOptions["orgId"].(int64)
  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,6 +81,7 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error) @@ -78,6 +81,7 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error)
78 return role, err 81 return role, err
79 } 82 }
80 } else { 83 } else {
  84 + queryFunc := func() (interface{}, error) {
81 if _, err := tx.QueryOne( 85 if _, err := tx.QueryOne(
82 pg.Scan( 86 pg.Scan(
83 &role.RoleId, 87 &role.RoleId,
@@ -105,6 +109,11 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error) @@ -105,6 +109,11 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error)
105 ); err != nil { 109 ); err != nil {
106 return role, err 110 return role, err
107 } 111 }
  112 + return role, nil
  113 + }
  114 + if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil {
  115 + return role, err
  116 + }
108 } 117 }
109 return role, nil 118 return role, nil
110 } 119 }
@@ -112,14 +121,21 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error @@ -112,14 +121,21 @@ 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)
  124 + queryFunc := func() (interface{}, error) {
115 if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil { 125 if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil {
116 return role, err 126 return role, err
117 } 127 }
118 return role, nil 128 return role, nil
  129 + }
  130 + if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil {
  131 + return role, err
  132 + }
  133 + return role, nil
119 } 134 }
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)
  138 + queryFunc := func() (interface{}, error) {
123 query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions) 139 query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions)
124 query.SetWhereByQueryOption("role_id = ?", "roleId") 140 query.SetWhereByQueryOption("role_id = ?", "roleId")
125 query.SetWhereByQueryOption("company_id = ?", "companyId") 141 query.SetWhereByQueryOption("company_id = ?", "companyId")
@@ -136,6 +152,17 @@ func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) ( @@ -136,6 +152,17 @@ func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (
136 return nil, err 152 return nil, err
137 } 153 }
138 } 154 }
  155 + return roleModel, nil
  156 + }
  157 +
  158 + var cacheModel = &domain.Role{}
  159 + if _, ok := queryOptions["roleId"]; ok {
  160 + cacheModel.RoleId = queryOptions["roleId"].(int64)
  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,6 +83,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U @@ -80,6 +83,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
80 return userBase, err 83 return userBase, err
81 } 84 }
82 } else { 85 } else {
  86 + queryFunc := func() (interface{}, error) {
83 if _, err := tx.QueryOne( 87 if _, err := tx.QueryOne(
84 pg.Scan( 88 pg.Scan(
85 &userBase.UserBaseId, 89 &userBase.UserBaseId,
@@ -105,6 +109,11 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U @@ -105,6 +109,11 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
105 ); err != nil { 109 ); err != nil {
106 return userBase, err 110 return userBase, err
107 } 111 }
  112 + return userBase, nil
  113 + }
  114 + if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil {
  115 + return userBase, err
  116 + }
108 } 117 }
109 return userBase, nil 118 return userBase, nil
110 } 119 }
@@ -112,14 +121,21 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain @@ -112,14 +121,21 @@ 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)
  124 + queryFunc := func() (interface{}, error) {
115 if _, err := tx.Model(userBaseModel).WherePK().Delete(); err != nil { 125 if _, err := tx.Model(userBaseModel).WherePK().Delete(); err != nil {
116 return userBase, err 126 return userBase, err
117 } 127 }
118 return userBase, nil 128 return userBase, nil
  129 + }
  130 + if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil {
  131 + return userBase, err
  132 + }
  133 + return userBase, nil
119 } 134 }
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)
  138 + queryFunc := func() (interface{}, error) {
123 query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions) 139 query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions)
124 query.SetWhereByQueryOption("account = ?", "account") 140 query.SetWhereByQueryOption("account = ?", "account")
125 query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId") 141 query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId")
@@ -130,6 +146,16 @@ func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{ @@ -130,6 +146,16 @@ func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{
130 return nil, err 146 return nil, err
131 } 147 }
132 } 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
  158 + }
133 if userBaseModel.UserBaseId == 0 { 159 if userBaseModel.UserBaseId == 0 {
134 return nil, nil 160 return nil, nil
135 } else { 161 } else {
@@ -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,6 +96,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) @@ -93,6 +96,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
93 return user, err 96 return user, err
94 } 97 }
95 } else { 98 } else {
  99 + queryFunc := func() (interface{}, error) {
96 if _, err := tx.QueryOne( 100 if _, err := tx.QueryOne(
97 pg.Scan( 101 pg.Scan(
98 &user.UserId, 102 &user.UserId,
@@ -130,6 +134,11 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) @@ -130,6 +134,11 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
130 ); err != nil { 134 ); err != nil {
131 return user, err 135 return user, err
132 } 136 }
  137 + return user, nil
  138 + }
  139 + if _, err := repository.Query(queryFunc, user.CacheKeyFunc()); err != nil {
  140 + return user, err
  141 + }
133 } 142 }
134 return user, nil 143 return user, nil
135 } 144 }
@@ -137,14 +146,21 @@ func (repository *UserRepository) Remove(user *domain.User) (*domain.User, error @@ -137,14 +146,21 @@ 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)
  149 + queryFunc := func() (interface{}, error) {
140 if _, err := tx.Model(userModel).WherePK().Delete(); err != nil { 150 if _, err := tx.Model(userModel).WherePK().Delete(); err != nil {
141 return user, err 151 return user, err
142 } 152 }
143 return user, nil 153 return user, nil
  154 + }
  155 + if _, err := repository.Query(queryFunc, user.CacheKeyFunc()); err != nil {
  156 + return user, err
  157 + }
  158 + return user, nil
144 } 159 }
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)
  163 + queryFunc := func() (interface{}, error) {
148 query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions) 164 query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions)
149 query.SetWhereByQueryOption("user_id = ?", "userId") 165 query.SetWhereByQueryOption("user_id = ?", "userId")
150 query.SetWhereByQueryOption("company_id=?", "companyId") 166 query.SetWhereByQueryOption("company_id=?", "companyId")
@@ -157,6 +173,15 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) ( @@ -157,6 +173,15 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (
157 return nil, err 173 return nil, err
158 } 174 }
159 } 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
  184 + }
160 if userModel.UserId == 0 { 185 if userModel.UserId == 0 {
161 return nil, nil 186 return nil, nil
162 } else { 187 } else {
@@ -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 }