This commit is contained in:
@@ -9,6 +9,19 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (m *defaultUserModel) execSubscribeMutation(ctx context.Context, cacheModels []*Subscribe, execFn func(conn *gorm.DB) error, tx ...*gorm.DB) error {
|
||||
defer func() {
|
||||
_ = m.ClearSubscribeCacheByModels(ctx, cacheModels...)
|
||||
}()
|
||||
|
||||
return m.ExecNoCacheCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return execFn(conn)
|
||||
})
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) UpdateUserSubscribeCache(ctx context.Context, data *Subscribe) error {
|
||||
return m.ClearSubscribeCacheByModels(ctx, data)
|
||||
}
|
||||
@@ -146,19 +159,9 @@ func (m *defaultUserModel) UpdateSubscribe(ctx context.Context, data *Subscribe,
|
||||
return err
|
||||
}
|
||||
|
||||
// 使用 defer 确保更新后清理缓存
|
||||
defer func() {
|
||||
if clearErr := m.ClearSubscribeCacheByModels(ctx, old, data); clearErr != nil {
|
||||
// 记录清理缓存错误
|
||||
}
|
||||
}()
|
||||
|
||||
return m.ExecNoCacheCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return m.execSubscribeMutation(ctx, []*Subscribe{old, data}, func(conn *gorm.DB) error {
|
||||
return conn.Model(&Subscribe{}).Where("id = ?", data.Id).Save(data).Error
|
||||
})
|
||||
}, tx...)
|
||||
}
|
||||
|
||||
// DeleteSubscribe deletes a record.
|
||||
@@ -168,36 +171,16 @@ func (m *defaultUserModel) DeleteSubscribe(ctx context.Context, token string, tx
|
||||
return err
|
||||
}
|
||||
|
||||
// 使用 defer 确保删除后清理缓存
|
||||
defer func() {
|
||||
if clearErr := m.ClearSubscribeCacheByModels(ctx, data); clearErr != nil {
|
||||
// 记录清理缓存错误
|
||||
}
|
||||
}()
|
||||
|
||||
return m.ExecNoCacheCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return m.execSubscribeMutation(ctx, []*Subscribe{data}, func(conn *gorm.DB) error {
|
||||
return conn.Where("token = ?", token).Delete(&Subscribe{}).Error
|
||||
})
|
||||
}, tx...)
|
||||
}
|
||||
|
||||
// InsertSubscribe insert Subscribe into the database.
|
||||
func (m *defaultUserModel) InsertSubscribe(ctx context.Context, data *Subscribe, tx ...*gorm.DB) error {
|
||||
// 使用 defer 确保插入后清理相关缓存
|
||||
defer func() {
|
||||
if clearErr := m.ClearSubscribeCacheByModels(ctx, data); clearErr != nil {
|
||||
// 记录清理缓存错误
|
||||
}
|
||||
}()
|
||||
|
||||
return m.ExecNoCacheCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return m.execSubscribeMutation(ctx, []*Subscribe{data}, func(conn *gorm.DB) error {
|
||||
return conn.Create(data).Error
|
||||
})
|
||||
}, tx...)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) DeleteSubscribeById(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
||||
@@ -206,19 +189,9 @@ func (m *defaultUserModel) DeleteSubscribeById(ctx context.Context, id int64, tx
|
||||
return err
|
||||
}
|
||||
|
||||
// 使用 defer 确保删除后清理缓存
|
||||
defer func() {
|
||||
if clearErr := m.ClearSubscribeCacheByModels(ctx, data); clearErr != nil {
|
||||
// 记录清理缓存错误
|
||||
}
|
||||
}()
|
||||
|
||||
return m.ExecNoCacheCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return m.execSubscribeMutation(ctx, []*Subscribe{data}, func(conn *gorm.DB) error {
|
||||
return conn.Where("id = ?", id).Delete(&Subscribe{}).Error
|
||||
})
|
||||
}, tx...)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) ClearSubscribeCache(ctx context.Context, data ...*Subscribe) error {
|
||||
|
||||
Reference in New Issue
Block a user