Example #1
0
File: album.go Project: dpup/dbps
func NewAlbum(folder string, dropbox *dropbox.Dropbox) *Album {
	a := &Album{folder: folder, dropbox: dropbox, cache: rcache.New(folder)}
	a.cache.RegisterFetcher(a.fetchOriginal)
	a.cache.RegisterFetcher(a.fetchThumbnail)

	expvar.Publish(fmt.Sprintf("photos (%s)", folder), expvar.Func(func() interface{} {
		return a.photoMap
	}))

	return a
}
Example #2
0
// The following example (pretends to) request images for a car, then has a
// dependent cache for thumbnails. If the original image is invalidated, so are
// the thumbnails.
func ExampleCache() {
	c := rcache.New("mycache")
	c.RegisterFetcher(func(key CarKey) ([]byte, error) {
		return getCarImage(key.Manufacturer, key.Model), nil
	})
	c.RegisterFetcher(func(key ThumbnailKey) ([]byte, error) {
		fullImage, _ := c.Get(CarKey{key.Manufacturer, key.Model})
		return resize(fullImage, key.Size), nil
	})

	// Original image is only fetched once.
	tx, _ := c.Get(CarKey{"BMW", "M5"})
	t200, _ := c.Get(ThumbnailKey{"BMW", "M5", 200})
	t400, _ := c.Get(ThumbnailKey{"BMW", "M5", 400})

	fmt.Printf("%s + %s + %s", tx, t200, t400)

	// Output: image:BMW.M5 + image:BMW.M5@200 + image:BMW.M5@400
}