forked from free-lancers/electricity_bill_calc_service
		
	enhance(cache):精简存在性缓存的键。
This commit is contained in:
		
							
								
								
									
										11
									
								
								cache/exists.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										11
									
								
								cache/exists.go
									
									
									
									
										vendored
									
									
								
							| @@ -13,9 +13,8 @@ func assembleExistsKey(entityName string) string { | ||||
| 	return CacheKey(TAG_EXISTS, keys...) | ||||
| } | ||||
|  | ||||
| func assembleExistsIdentification(id string, additional ...string) string { | ||||
| func assembleExistsIdentification(additional ...string) string { | ||||
| 	var b strings.Builder | ||||
| 	b.WriteString(id) | ||||
| 	for _, s := range additional { | ||||
| 		fmt.Fprintf(&b, ":%s", s) | ||||
| 	} | ||||
| @@ -23,17 +22,17 @@ func assembleExistsIdentification(id string, additional ...string) string { | ||||
| } | ||||
|  | ||||
| // 缓存模型名称明确的、包含指定ID以及一些附加条件的记录 | ||||
| func CacheExists(entityName, id string, conditions ...string) error { | ||||
| func CacheExists(entityName string, conditions ...string) error { | ||||
| 	existskey := assembleExistsKey(entityName) | ||||
| 	identification := assembleExistsIdentification(id, conditions...) | ||||
| 	identification := assembleExistsIdentification(conditions...) | ||||
| 	result := global.RedisConn.SAdd(global.Ctx, existskey, identification) | ||||
| 	return result.Err() | ||||
| } | ||||
|  | ||||
| // 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记,函数在返回false时不保证数据库中相关记录也不存在 | ||||
| func CheckExists(entityName, id string, condtions ...string) (bool, error) { | ||||
| func CheckExists(entityName string, condtions ...string) (bool, error) { | ||||
| 	existsKey := assembleExistsKey(entityName) | ||||
| 	identification := assembleExistsIdentification(id, condtions...) | ||||
| 	identification := assembleExistsIdentification(condtions...) | ||||
| 	result := global.RedisConn.SIsMember(global.Ctx, existsKey, identification) | ||||
| 	return result.Val(), result.Err() | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user