(key string, value string)
| 11 | ) |
| 12 | |
| 13 | func AppendBigStringToRedis(key string, value string) error { |
| 14 | redisCache := NewRedisCache(config.RedisCommonCacheTokenDB()) |
| 15 | |
| 16 | // 读取现有的日志内容 |
| 17 | existingCompressedLogStr, err := redisCache.GetString(key) |
| 18 | if err != nil && !errors.Is(err, redis.Nil) { |
| 19 | // 如果错误不是 key 不存在,则返回错误 |
| 20 | return fmt.Errorf("failed to read existing string from redis, key: %s, error: %s", key, err) |
| 21 | } |
| 22 | |
| 23 | // 追加新内容到现有日志 |
| 24 | var newLogContent string |
| 25 | if err == nil && existingCompressedLogStr != "" { |
| 26 | // 解压缩现有日志(将字符串转换为字节数组) |
| 27 | existingLog, decompressErr := util.DecompressBytes([]byte(existingCompressedLogStr)) |
| 28 | if decompressErr != nil { |
| 29 | return fmt.Errorf("failed to decompress existing string, key: %s, error: %s", key, decompressErr) |
| 30 | } |
| 31 | newLogContent = existingLog + value |
| 32 | } else { |
| 33 | newLogContent = value |
| 34 | } |
| 35 | |
| 36 | compressedLogBytes, err := util.CompressBytes(newLogContent) |
| 37 | if err != nil { |
| 38 | return fmt.Errorf("failed to compress string, key: %s, error: %s", key, err) |
| 39 | } |
| 40 | |
| 41 | // 写回 Redis |
| 42 | err = redisCache.Write(key, string(compressedLogBytes), 3*24*time.Hour) |
| 43 | if err != nil { |
| 44 | err = fmt.Errorf("failed to write string to redis, key: %s, error: %s", key, err) |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | func GetBigStringFromRedis(logKey string) (string, error) { |
| 52 | redisCache := NewRedisCache(config.RedisCommonCacheTokenDB()) |
no test coverage detected