mongodb基础操作方法增加redis缓存策略
This commit is contained in:
@@ -98,7 +98,6 @@ func Find(ctx context.Context, filter bson.M, result interface{}, collection str
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
filter["isDeleted"] = false
|
filter["isDeleted"] = false
|
||||||
filter["tenantId"] = user.TenantId
|
|
||||||
filterMap := utils.OrderMap(filter)
|
filterMap := utils.OrderMap(filter)
|
||||||
optsMap := listOptionsToMap(ctx, opts...)
|
optsMap := listOptionsToMap(ctx, opts...)
|
||||||
redisKey := fmt.Sprintf(consts.List, user.TenantId, collection, gconv.String(filterMap), gconv.String(optsMap))
|
redisKey := fmt.Sprintf(consts.List, user.TenantId, collection, gconv.String(filterMap), gconv.String(optsMap))
|
||||||
@@ -113,6 +112,8 @@ func Find(ctx context.Context, filter bson.M, result interface{}, collection str
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filter["tenantId"] = user.TenantId
|
||||||
cur, err := db.Collection(collection).Find(ctx, filter, opts...)
|
cur, err := db.Collection(collection).Find(ctx, filter, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -139,7 +140,6 @@ func FindOne(ctx context.Context, filter bson.M, result interface{}, collection
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
filter["isDeleted"] = false
|
filter["isDeleted"] = false
|
||||||
filter["tenantId"] = user.TenantId
|
|
||||||
filterMap := utils.OrderMap(filter)
|
filterMap := utils.OrderMap(filter)
|
||||||
optsMap := oneOptionsToMap(ctx, opts...)
|
optsMap := oneOptionsToMap(ctx, opts...)
|
||||||
redisKey := fmt.Sprintf(consts.One, user.TenantId, collection, gconv.String(filterMap), gconv.String(optsMap))
|
redisKey := fmt.Sprintf(consts.One, user.TenantId, collection, gconv.String(filterMap), gconv.String(optsMap))
|
||||||
@@ -154,6 +154,7 @@ func FindOne(ctx context.Context, filter bson.M, result interface{}, collection
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
filter["tenantId"] = user.TenantId
|
||||||
cur := db.Collection(collection).FindOne(ctx, filter, opts...)
|
cur := db.Collection(collection).FindOne(ctx, filter, opts...)
|
||||||
err = cur.Decode(result)
|
err = cur.Decode(result)
|
||||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
if errors.Is(err, mongo.ErrNoDocuments) {
|
||||||
@@ -165,6 +166,31 @@ func FindOne(ctx context.Context, filter bson.M, result interface{}, collection
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
func cleanRedis(ctx context.Context, tenantId interface{}, collection string) (err error) {
|
||||||
|
listKeys := fmt.Sprintf(consts.CleanList, tenantId, collection)
|
||||||
|
keys, err := redis.RedisClient.Keys(ctx, listKeys)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, key := range keys {
|
||||||
|
_, err = redis.RedisClient.Del(ctx, key)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
countKeys := fmt.Sprintf(consts.CleanCount, tenantId, collection)
|
||||||
|
keys, err = redis.RedisClient.Keys(ctx, countKeys)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, key := range keys {
|
||||||
|
_, err = redis.RedisClient.Del(ctx, key)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Delete 删除记录
|
// Delete 删除记录
|
||||||
func Delete(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.DeleteManyOptions]) (count int64, err error) {
|
func Delete(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.DeleteManyOptions]) (count int64, err error) {
|
||||||
@@ -182,14 +208,7 @@ func Delete(ctx context.Context, filter bson.M, collection string, opts ...optio
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
count = r.DeletedCount
|
count = r.DeletedCount
|
||||||
_, err = redis.RedisClient.Del(ctx, fmt.Sprintf(consts.CleanList, user.TenantId, collection))
|
err = cleanRedis(ctx, user.TenantId, collection)
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = redis.RedisClient.Del(ctx, fmt.Sprintf(consts.CleanCount, user.TenantId, collection))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,14 +232,7 @@ func Update(ctx context.Context, filter bson.M, update bson.M, collection string
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = redis.RedisClient.Del(ctx, fmt.Sprintf(consts.CleanList, user.TenantId, collection))
|
err = cleanRedis(ctx, user.TenantId, collection)
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = redis.RedisClient.Del(ctx, fmt.Sprintf(consts.CleanCount, user.TenantId, collection))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,14 +259,7 @@ func Insert(ctx context.Context, documents []interface{}, collection string, opt
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ids = r.InsertedIDs
|
ids = r.InsertedIDs
|
||||||
_, err = redis.RedisClient.Del(ctx, fmt.Sprintf(consts.CleanList, user.TenantId, collection))
|
err = cleanRedis(ctx, user.TenantId, collection)
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = redis.RedisClient.Del(ctx, fmt.Sprintf(consts.CleanCount, user.TenantId, collection))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user