MCPcopy Create free account
hub / github.com/koderover/zadig / AppendBigStringToRedis

Function AppendBigStringToRedis

pkg/tool/cache/redis_string.go:13–49  ·  view source on GitHub ↗
(key string, value string)

Source from the content-addressed store, hash-verified

11)
12
13func 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
51func GetBigStringFromRedis(logKey string) (string, error) {
52 redisCache := NewRedisCache(config.RedisCommonCacheTokenDB())

Callers 3

SaveJobLogMethod · 0.92
SaveJobLogNoTimeMethod · 0.92
savaVMJobLogFunction · 0.92

Calls 7

GetStringMethod · 0.95
WriteMethod · 0.95
RedisCommonCacheTokenDBFunction · 0.92
DecompressBytesFunction · 0.92
CompressBytesFunction · 0.92
NewRedisCacheFunction · 0.70
ErrorfMethod · 0.45

Tested by

no test coverage detected