// Set implements Cache. func (cache *Cache) Set(key string, im *imageserver.Image, params imageserver.Params) error { data, err := im.MarshalBinary() if err != nil { return err } return cache.setData(key, data) }
func parseResponse(response *http.Response) (*imageserver.Image, error) { if response.StatusCode != http.StatusOK { return nil, &imageserver.ParamError{ Param: imageserver.SourceParam, Message: fmt.Sprintf("http status code %d while downloading", response.StatusCode), } } im := new(imageserver.Image) contentType := response.Header.Get("Content-Type") if contentType != "" { matches := contentTypeRegexp.FindStringSubmatch(contentType) if matches != nil && len(matches) == 2 { im.Format = matches[1] } } data, err := ioutil.ReadAll(response.Body) if err != nil { return nil, &imageserver.ParamError{ Param: imageserver.SourceParam, Message: fmt.Sprintf("error while downloading: %s", err), } } im.Data = data return im, nil }
// Get implements Cache. func (cache *Cache) Get(key string, params imageserver.Params) (*imageserver.Image, error) { data, err := cache.getData(key) if err != nil { return nil, err } if data == nil { return nil, nil } im := new(imageserver.Image) err = im.UnmarshalBinaryNoCopy(data) if err != nil { return nil, err } return im, nil }
// Get implements Server. func (srv *Server) Get(params imageserver.Params) (*imageserver.Image, error) { ctx := &Context{ Params: params, } key := srv.KeyGenerator.GetKey(params) var data []byte dest := groupcache.AllocatingByteSliceSink(&data) err := srv.Group.Get(ctx, key, dest) if err != nil { return nil, err } im := new(imageserver.Image) err = im.UnmarshalBinaryNoCopy(data) if err != nil { return nil, err } return im, nil }
func TestGetter(t *testing.T) { ctx := &Context{ Params: imageserver.Params{}, } var data []byte dest := groupcache.AllocatingByteSliceSink(&data) gt := &Getter{ Server: &imageserver.StaticServer{ Image: testdata.Medium, }, } err := gt.Get(ctx, "foo", dest) if err != nil { t.Fatal(err) } im := new(imageserver.Image) err = im.UnmarshalBinary(data) if err != nil { t.Fatal(err) } if !imageserver.ImageEqual(im, testdata.Medium) { t.Fatal("not equal") } }