// Retrieve a cache item from the cache. func (r *Redis) Get(key string, items ...kit.CacheItem) (kit.CacheItem, Error) { var item kit.CacheItem = &StrItem{} if items != nil { if len(items) != 1 { return nil, kit.AppError{ Code: "invalid_item", Message: "You must specify one item only", Internal: true, } } item = items[0] } if key == "" { return nil, kit.AppError{Code: "empty_key"} } item.SetKey(key) key = r.key(key) conn := r.pool.Get() defer conn.Close() result, err := redis.Strings(conn.Do("MGET", key, r.tagKey(key))) if err != nil { return nil, redisErr(err) } // Retrieve the value. if result[0] == "" { return nil, nil } if err := item.FromString(result[0]); err != nil { return nil, kit.AppError{ Code: "cacheitem_fromstring_error", Message: err.Error(), Errors: []error{err}, } } // Parse and set tags. if result[1] != "" { item.SetTags(strings.Split(result[1], ";")) } // Now get the ttl. ttl, err := redis.Int(conn.Do("TTL", key)) if err == nil && ttl > 0 { expires := time.Now().Add(time.Duration(ttl) * time.Second) item.SetExpiresAt(expires) } else if err != nil && err != redis.ErrNil { return nil, redisErr(err) } // Return nil if item is expired. if item.IsExpired() { return nil, nil } return item, nil }
// Retrieve a cache item from the cache. func (fs *Fs) Get(key string, items ...kit.CacheItem) (kit.CacheItem, apperror.Error) { var item kit.CacheItem = &StrItem{} if items != nil { if len(items) != 1 { return nil, &apperror.Err{ Code: "invalid_item", Message: "You must specify one item only", } } item = items[0] } key = fs.key(key) if key == "" { return nil, apperror.New("empty_key") } exists, err := utils.FileExists(fs.keyPath(key)) if err != nil { return nil, err } if !exists { return nil, nil } metaContent, err := utils.ReadFile(fs.keyMetaPath(key)) if err != nil { return nil, err } if err := json.Unmarshal(metaContent, &item); err != nil { return nil, apperror.Wrap(err, "metadata_unmarshal_error") } // Reset ExpiresAt if it is zero, since json.Unmarshal produces // a time.Time which is not exactly equal to the zero value. if item.GetExpiresAt().IsZero() { item.SetExpiresAt(time.Time{}) } content, err := utils.ReadFile(fs.keyPath(key)) if err != nil { return nil, err } if err := item.FromString(string(content)); err != nil { return nil, apperror.Wrap(err, "cacheitem_fromstring_error") } // Return nil if item is expired. if item.IsExpired() { return nil, nil } return item, nil }