MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / RedisHSetObj

Function RedisHSetObj

common/redis.go:107–159  ·  view source on GitHub ↗
(key string, obj interface{}, expiration time.Duration)

Source from the content-addressed store, hash-verified

105}
106
107func RedisHSetObj(key string, obj interface{}, expiration time.Duration) error {
108 if DebugEnabled {
109 SysLog(fmt.Sprintf("Redis HSET: key=%s, obj=%+v, expiration=%v", key, obj, expiration))
110 }
111 ctx := context.Background()
112
113 data := make(map[string]interface{})
114
115 // 使用反射遍历结构体字段
116 v := reflect.ValueOf(obj).Elem()
117 t := v.Type()
118 for i := 0; i < v.NumField(); i++ {
119 field := t.Field(i)
120 value := v.Field(i)
121
122 // Skip DeletedAt field
123 if field.Type.String() == "gorm.DeletedAt" {
124 continue
125 }
126
127 // 处理指针类型
128 if value.Kind() == reflect.Ptr {
129 if value.IsNil() {
130 data[field.Name] = ""
131 continue
132 }
133 value = value.Elem()
134 }
135
136 // 处理布尔类型
137 if value.Kind() == reflect.Bool {
138 data[field.Name] = strconv.FormatBool(value.Bool())
139 continue
140 }
141
142 // 其他类型直接转换为字符串
143 data[field.Name] = fmt.Sprintf("%v", value.Interface())
144 }
145
146 txn := RDB.TxPipeline()
147 txn.HSet(ctx, key, data)
148
149 // 只有在 expiration 大于 0 时才设置过期时间
150 if expiration > 0 {
151 txn.Expire(ctx, key, expiration)
152 }
153
154 _, err := txn.Exec(ctx)
155 if err != nil {
156 return fmt.Errorf("failed to execute transaction: %w", err)
157 }
158 return nil
159}
160
161func RedisHGetObj(key string, obj interface{}) error {
162 if DebugEnabled {

Callers 2

updateUserCacheFunction · 0.92
cacheSetTokenFunction · 0.92

Calls 1

SysLogFunction · 0.85

Tested by

no test coverage detected