forked from free-lancers/electricity_bill_calc_service
		
	fix(cache):修正对于过期时间的处理,使缓存内容可以正常的保存。
This commit is contained in:
		
							
								
								
									
										18
									
								
								cache/abstract.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										18
									
								
								cache/abstract.go
									
									
									
									
										vendored
									
									
								
							| @@ -21,11 +21,19 @@ const ( | ||||
| // 向Redis缓存中保存一个数据 | ||||
| // ! 如果需要长期保存一个数据,那么需要向expires传入0。 | ||||
| func Cache[T interface{}](key string, value *T, expires time.Duration) error { | ||||
| 	setCmd := global.RedisConn.B().Set(). | ||||
| 		Key(key).Value(rueidis.JSON(value)). | ||||
| 		ExSeconds(int64(expires.Seconds())). | ||||
| 		Build() | ||||
| 	err := global.RedisConn.Do(global.Ctx, setCmd).Error() | ||||
| 	var err error | ||||
| 	if expires > 0 { | ||||
| 		setCmd := global.RedisConn.B().Set(). | ||||
| 			Key(key).Value(rueidis.JSON(value)). | ||||
| 			ExSeconds(int64(expires.Seconds())). | ||||
| 			Build() | ||||
| 		err = global.RedisConn.Do(global.Ctx, setCmd).Error() | ||||
| 	} else { | ||||
| 		setCmd := global.RedisConn.B().Set(). | ||||
| 			Key(key).Value(rueidis.JSON(value)). | ||||
| 			Build() | ||||
| 		err = global.RedisConn.Do(global.Ctx, setCmd).Error() | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										2
									
								
								cache/entity.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								cache/entity.go
									
									
									
									
										vendored
									
									
								
							| @@ -19,7 +19,7 @@ func assembleEntityKey(entityName, id string) string { | ||||
| // 缓存模型名称明确的,使用ID进行检索的实体内容。 | ||||
| func CacheEntity[T any](instance T, relationNames []string, entityName, id string) error { | ||||
| 	entityKey := assembleEntityKey(entityName, id) | ||||
| 	err := Cache(entityKey, &instance, 0) | ||||
| 	err := Cache(entityKey, &instance, -1) | ||||
| 	for _, relationName := range relationNames { | ||||
| 		CacheRelation(relationName, STORE_TYPE_KEY, entityKey) | ||||
| 	} | ||||
|   | ||||
							
								
								
									
										2
									
								
								cache/exists.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								cache/exists.go
									
									
									
									
										vendored
									
									
								
							| @@ -33,7 +33,7 @@ func CheckExists(entityName string, condtions ...string) (bool, error) { | ||||
| 	existsKey := assembleExistsKey(entityName) | ||||
| 	identification := assembleExistsIdentification(condtions...) | ||||
| 	cmd := global.RedisConn.B().Sismember().Key(existsKey).Member(identification).Build() | ||||
| 	result, err := global.RedisConn.Do(global.Ctx, cmd).ToBool() | ||||
| 	result, err := global.RedisConn.Do(global.Ctx, cmd).AsBool() | ||||
| 	return result, err | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										2
									
								
								cache/search.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								cache/search.go
									
									
									
									
										vendored
									
									
								
							| @@ -20,7 +20,7 @@ func assembleSearchKey(entityName string, additional ...string) string { | ||||
| // 缓存模型名称明确的,使用或者包含非ID检索条件的实体内容。 | ||||
| func CacheSearch[T any](instance T, relationNames []string, entityName string, conditions ...string) error { | ||||
| 	searchKey := assembleSearchKey(entityName, conditions...) | ||||
| 	err := Cache(searchKey, &instance, 0) | ||||
| 	err := Cache(searchKey, &instance, -1) | ||||
| 	for _, relationName := range relationNames { | ||||
| 		CacheRelation(relationName, STORE_TYPE_KEY, searchKey) | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user