Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cache/afterDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ func AfterDelete(cache *Gorm2Cache) func(db *gorm.DB) {
cache.Logger.CtxInfo(ctx, "[AfterDelete] invalidating all primary cache for table: %s finished.", tableName)
}

// 失效unique键缓存
// 尝试从WHERE子句中提取unique键
uniqueKeysMap, _ := getUniqueKeysFromWhereClause(db)
if len(uniqueKeysMap) > 0 {
for indexName, uniqueKeys := range uniqueKeysMap {
if len(uniqueKeys) > 0 {
cache.Logger.CtxInfo(ctx, "[AfterDelete] now start to invalidate unique cache for index %s keys: %+v", indexName, uniqueKeys)
err := cache.BatchInvalidateUniqueCache(ctx, tableName, indexName, uniqueKeys)
if err != nil {
cache.Logger.CtxError(ctx, "[AfterDelete] invalidating unique cache for index %s keys %v error: %v",
indexName, uniqueKeys, err)
} else {
cache.Logger.CtxInfo(ctx, "[AfterDelete] invalidating unique cache for index %s keys: %+v finished.", indexName, uniqueKeys)
}
}
}
} else {
// 如果没有从WHERE子句提取到unique键,失效所有unique键缓存
if db.Statement.Schema != nil {
allUniqueIndexes := getAllUniqueIndexes(db.Statement.Schema)
for indexName := range allUniqueIndexes {
cache.Logger.CtxInfo(ctx, "[AfterDelete] now start to invalidate all unique cache for index %s", indexName)
err := cache.InvalidateAllUniqueCache(ctx, tableName, indexName)
if err != nil {
cache.Logger.CtxError(ctx, "[AfterDelete] invalidating all unique cache for index %s error: %v", indexName, err)
} else {
cache.Logger.CtxInfo(ctx, "[AfterDelete] invalidating all unique cache for index %s finished.", indexName)
}
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}
}()

Expand Down
32 changes: 32 additions & 0 deletions cache/afterUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ func AfterUpdate(cache *Gorm2Cache) func(db *gorm.DB) {
}
cache.Logger.CtxInfo(ctx, "[AfterUpdate] invalidating all primary cache for table: %s finished.", tableName)
}

// 失效unique键缓存
// 尝试从WHERE子句中提取unique键
uniqueKeysMap, _ := getUniqueKeysFromWhereClause(db)
if len(uniqueKeysMap) > 0 {
for indexName, uniqueKeys := range uniqueKeysMap {
if len(uniqueKeys) > 0 {
cache.Logger.CtxInfo(ctx, "[AfterUpdate] now start to invalidate unique cache for index %s keys: %+v", indexName, uniqueKeys)
err := cache.BatchInvalidateUniqueCache(ctx, tableName, indexName, uniqueKeys)
if err != nil {
cache.Logger.CtxError(ctx, "[AfterUpdate] invalidating unique cache for index %s keys %v error: %v",
indexName, uniqueKeys, err)
} else {
cache.Logger.CtxInfo(ctx, "[AfterUpdate] invalidating unique cache for index %s keys: %+v finished.", indexName, uniqueKeys)
}
}
}
} else {
// 如果没有从WHERE子句提取到unique键,失效所有unique键缓存
if db.Statement.Schema != nil {
allUniqueIndexes := getAllUniqueIndexes(db.Statement.Schema)
for indexName := range allUniqueIndexes {
cache.Logger.CtxInfo(ctx, "[AfterUpdate] now start to invalidate all unique cache for index %s", indexName)
err := cache.InvalidateAllUniqueCache(ctx, tableName, indexName)
if err != nil {
cache.Logger.CtxError(ctx, "[AfterUpdate] invalidating all unique cache for index %s error: %v", indexName, err)
} else {
cache.Logger.CtxInfo(ctx, "[AfterUpdate] invalidating all unique cache for index %s finished.", indexName)
}
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}
}()

Expand Down
46 changes: 46 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ func (c *Gorm2Cache) InvalidateSearchCache(ctx context.Context, tableName string
}

func (c *Gorm2Cache) InvalidatePrimaryCache(ctx context.Context, tableName string, primaryKey string) error {
// primaryKey 已经是最终格式(单个值或已用":"连接的联合主键),直接传入
return c.cache.DeleteKey(ctx, util.GenPrimaryCacheKey(c.InstanceId, tableName, primaryKey))
}

func (c *Gorm2Cache) BatchInvalidatePrimaryCache(ctx context.Context, tableName string, primaryKeys []string) error {
cacheKeys := make([]string, 0, len(primaryKeys))
for _, primaryKey := range primaryKeys {
// primaryKey 已经是最终格式(单个值或已用":"连接的联合主键),直接传入
cacheKeys = append(cacheKeys, util.GenPrimaryCacheKey(c.InstanceId, tableName, primaryKey))
}
return c.cache.BatchDeleteKeys(ctx, cacheKeys)
Expand All @@ -135,6 +137,7 @@ func (c *Gorm2Cache) InvalidateAllPrimaryCache(ctx context.Context, tableName st
func (c *Gorm2Cache) BatchPrimaryKeyExists(ctx context.Context, tableName string, primaryKeys []string) (bool, error) {
cacheKeys := make([]string, 0, len(primaryKeys))
for _, primaryKey := range primaryKeys {
// primaryKey 已经是最终格式(单个值或已用":"连接的联合主键),直接传入
cacheKeys = append(cacheKeys, util.GenPrimaryCacheKey(c.InstanceId, tableName, primaryKey))
}
return c.cache.BatchKeyExist(ctx, cacheKeys)
Expand All @@ -147,6 +150,7 @@ func (c *Gorm2Cache) SearchKeyExists(ctx context.Context, tableName string, SQL

func (c *Gorm2Cache) BatchSetPrimaryKeyCache(ctx context.Context, tableName string, kvs []util.Kv) error {
for idx, kv := range kvs {
// kv.Key 已经是最终格式(单个值或已用":"连接的联合主键),直接传入
kvs[idx].Key = util.GenPrimaryCacheKey(c.InstanceId, tableName, kv.Key)
}
return c.cache.BatchSetKeys(ctx, kvs)
Expand All @@ -169,7 +173,49 @@ func (c *Gorm2Cache) GetSearchCache(ctx context.Context, tableName string, sql s
func (c *Gorm2Cache) BatchGetPrimaryCache(ctx context.Context, tableName string, primaryKeys []string) ([]string, error) {
cacheKeys := make([]string, 0, len(primaryKeys))
for _, primaryKey := range primaryKeys {
// primaryKey 已经是最终格式(单个值或已用":"连接的联合主键),直接传入
cacheKeys = append(cacheKeys, util.GenPrimaryCacheKey(c.InstanceId, tableName, primaryKey))
}
return c.cache.BatchGetValues(ctx, cacheKeys)
}

// BatchGetUniqueCache 批量获取unique键缓存
func (c *Gorm2Cache) BatchGetUniqueCache(ctx context.Context, tableName string, uniqueIndexName string, uniqueKeys []string) ([]string, error) {
cacheKeys := make([]string, 0, len(uniqueKeys))
for _, uniqueKey := range uniqueKeys {
// uniqueKey 已经是最终格式(单个值或已用":"连接的联合unique键),直接传入
cacheKeys = append(cacheKeys, util.GenUniqueCacheKey(c.InstanceId, tableName, uniqueIndexName, uniqueKey))
}
return c.cache.BatchGetValues(ctx, cacheKeys)
}

// BatchSetUniqueCache 批量设置unique键缓存。
// 注意:会原地修改 kvs 中每个元素的 Key 为完整缓存 key,调用方不应再使用传入的 kvs。
func (c *Gorm2Cache) BatchSetUniqueCache(ctx context.Context, tableName string, uniqueIndexName string, kvs []util.Kv) error {
for idx, kv := range kvs {
// kv.Key 已经是最终格式(单个值或已用":"连接的联合unique键),直接传入
kvs[idx].Key = util.GenUniqueCacheKey(c.InstanceId, tableName, uniqueIndexName, kv.Key)
}
return c.cache.BatchSetKeys(ctx, kvs)
}

// InvalidateUniqueCache 失效unique键缓存
func (c *Gorm2Cache) InvalidateUniqueCache(ctx context.Context, tableName string, uniqueIndexName string, uniqueKey string) error {
// uniqueKey 已经是最终格式(单个值或已用":"连接的联合unique键),直接传入
return c.cache.DeleteKey(ctx, util.GenUniqueCacheKey(c.InstanceId, tableName, uniqueIndexName, uniqueKey))
}

// BatchInvalidateUniqueCache 批量失效unique键缓存
func (c *Gorm2Cache) BatchInvalidateUniqueCache(ctx context.Context, tableName string, uniqueIndexName string, uniqueKeys []string) error {
cacheKeys := make([]string, 0, len(uniqueKeys))
for _, uniqueKey := range uniqueKeys {
// uniqueKey 已经是最终格式(单个值或已用":"连接的联合unique键),直接传入
cacheKeys = append(cacheKeys, util.GenUniqueCacheKey(c.InstanceId, tableName, uniqueIndexName, uniqueKey))
}
return c.cache.BatchDeleteKeys(ctx, cacheKeys)
}

// InvalidateAllUniqueCache 失效所有unique键缓存
func (c *Gorm2Cache) InvalidateAllUniqueCache(ctx context.Context, tableName string, uniqueIndexName string) error {
return c.cache.DeleteKeysWithPrefix(ctx, util.GenUniqueCachePrefix(c.InstanceId, tableName, uniqueIndexName))
}
Loading