// Save a new item into the cache. func (r *Redis) Set(item kit.CacheItem) Error { key := item.GetKey() if key == "" { return kit.AppError{Code: "empty_key"} } key = r.key(key) if item.IsExpired() { return kit.AppError{Code: "item_expired"} } expireSeconds := 0 if expires := item.GetExpiresAt(); !expires.IsZero() { seconds := expires.Sub(time.Now()).Seconds() if seconds > 0 && seconds < 1 { return kit.AppError{Code: "item_expired"} } expireSeconds = int(seconds) } conn := r.pool.Get() defer conn.Close() value, err := item.ToString() if err != nil { return kit.AppError{ Code: "cacheitem_tostring_error", Message: err.Error(), Errors: []error{err}, } } if value == "" { return kit.AppError{Code: "empty_value"} } conn.Send("SET", key, value) if expireSeconds > 0 { conn.Send("EXPIRE", key, expireSeconds) } if tags := item.GetTags(); tags != nil { tagKey := r.tagKey(key) conn.Send("SET", tagKey, strings.Join(tags, ";")) if expireSeconds > 0 { conn.Send("EXPIRE", tagKey, expireSeconds) } } if err := conn.Flush(); err != nil { return redisErr(err) } return nil }
// Save a new item into the cache. func (fs *Fs) Set(item kit.CacheItem) apperror.Error { key := fs.key(item.GetKey()) if key == "" { return apperror.New("empty_key") } if item.IsExpired() { return apperror.New("item_expired") } value, err := item.ToString() if err != nil { return apperror.Wrap(err, "cacheitem_tostring_error") } if value == "" { return apperror.New("empty_value") } // Marshal metadata. tmpVal := item.GetValue() item.SetValue(nil) js, err2 := json.Marshal(item) if err2 != nil { return apperror.Wrap(err2, "json_marshal_error") } item.SetValue(tmpVal) if err := utils.WriteFile(fs.keyPath(key), []byte(value), false); err != nil { return err } if err := utils.WriteFile(fs.keyMetaPath(key), js, false); err != nil { return err } return nil }