forked from free-lancers/electricity_bill_calc_service
		
	enhance(cache):增加一个批量删除指定模式键的函数。
This commit is contained in:
		
							
								
								
									
										22
									
								
								cache/abstract.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										22
									
								
								cache/abstract.go
									
									
									
									
										vendored
									
									
								
							| @@ -59,6 +59,28 @@ func Delete(key string) (bool, error) { | |||||||
| 	return result > 0, nil | 	return result > 0, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // 从Redis缓存中批量删除符合pattern的键,这里的pattern直接使用Redis的pattern规则 | ||||||
|  | func DeleteAll(pattern string) error { | ||||||
|  | 	var ( | ||||||
|  | 		cursor uint64 | ||||||
|  | 		keys   = make([]string, 0) | ||||||
|  | 	) | ||||||
|  | 	for { | ||||||
|  | 		k, cursor, err := global.RedisConn.Scan(global.Ctx, cursor, pattern, 20).Result() | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		keys = append(keys, k...) | ||||||
|  | 		if cursor == 0 { | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	pipeline := global.RedisConn.Pipeline() | ||||||
|  | 	pipeline.Del(global.Ctx, keys...) | ||||||
|  | 	_, err := pipeline.Exec(global.Ctx) | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
| // 生成用于Redis存储的键 | // 生成用于Redis存储的键 | ||||||
| func CacheKey(category string, ids ...string) string { | func CacheKey(category string, ids ...string) string { | ||||||
| 	var b strings.Builder | 	var b strings.Builder | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user