imagecache

package
v1.44.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package imagecache is the privacy proxy's store for remote mail images: a directory of files named by their URL's hash, shared by all messages, bounded in size and evicted least recently used first. Get downloads an image once (through imagefetch: public hosts, no cookies, no Referer) and serves it from disk after that, offline too.

Index

Constants

View Source
const (
	MaxBytes   = 256 << 20 // the cache's size
	GlobalJobs = 8         // downloads at once
	HostJobs   = 3         // downloads at once from one host

)

Limits of the fetching proxy.

Variables

View Source
var ErrClosed = errors.New("image cache is closed")

ErrClosed is returned by Get after Close.

Functions

func AvatarKey added in v0.3.0

func AvatarKey(source, identity string) string

AvatarKey identifies a source and URL (or normalized website host). Pass this complete key to PutAvatar, LookupAvatar and the miss methods.

func Key

func Key(src string) string

Key is the cache key of a URL: 32 hex digits of its SHA-256.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a directory of images; it is safe for concurrent use.

func Open

func Open(dir string, max int64, client *http.Client) (*Cache, error)

Open is the cache in dir (made if need be), at most max bytes (MaxBytes when 0), downloading with client.

func (*Cache) AvatarGeneration added in v0.3.0

func (c *Cache) AvatarGeneration() int

AvatarGeneration captures the Clear boundary before an avatar discovery starts.

func (*Cache) AvatarMiss added in v0.3.0

func (c *Cache) AvatarMiss(key string) bool

AvatarMiss reports a persistent deterministic miss, for at most 24 hours.

func (*Cache) AvatarUnreachable added in v0.3.0

func (c *Cache) AvatarUnreachable(key string) bool

AvatarUnreachable reports a discovery for key that failed short of a miss (no connection, a timeout) within the last few minutes, as Get does for URLs.

func (*Cache) Cached

func (c *Cache) Cached(src string) bool

Cached reports whether src's image is on disk.

func (*Cache) Clear

func (c *Cache) Clear() error

Clear deletes every cached image.

func (*Cache) Close

func (c *Cache) Close()

Close cancels the downloads in flight and waits for them; Get fails after.

func (*Cache) Dims

func (c *Cache) Dims(src string) (width, height int, ok bool)

Dims is the size of src's image if it is cached; it reads no file.

func (*Cache) FallbackAvatar added in v0.3.0

func (c *Cache) FallbackAvatar(ctx context.Context, key string, gen int) (Image, bool, error)

FallbackAvatar revalidates an ordinary avatar after a failed refresh, without changing its age. BIMI and entries invalidated by Clear are never returned.

func (*Cache) ForgetFailure added in v1.16.0

func (c *Cache) ForgetFailure(src string)

ForgetFailure lets the next Get of src try again at once, though src failed within failedTTL: an explicit retry.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, src string) (Image, error)

Get is src's image: from disk, or downloaded now (once, however many ask at the same time). A download goes on when its caller gives up, so the next ask finds it; a URL that failed fails at once for a few minutes.

func (*Cache) GetAvatar added in v0.3.0

func (c *Cache) GetAvatar(ctx context.Context, src, source string, gen int) (Image, error)

GetAvatar shares the ordinary cache's jobs, singleflight and lifecycle. The first caller's deadline bounds the shared download; cancellation stops waiting. gen is captured before discovery, so Clear also blocks its later candidate downloads.

func (*Cache) GetFile added in v1.36.0

func (c *Cache) GetFile(ctx context.Context, src string) (path, format string, width, height int, err error)

GetFile is Get returning the cached file's path on disk instead of reading its bytes on every hit; *NotStoredError when it could not be stored.

func (*Cache) LookupAvatar added in v0.3.0

func (c *Cache) LookupAvatar(ctx context.Context, key string) (Image, bool, error)

LookupAvatar reads only a fresh, revalidated avatar entry, without networking.

func (*Cache) PruneUnused added in v1.35.0

func (c *Cache) PruneUnused(cutoff time.Time) int

PruneUnused deletes every cached image that has not been read since cutoff, and reports how many it deleted. A read stamps the file, so the age survives a restart. A remembered miss is left to evict.

func (*Cache) PutAvatar added in v0.3.0

func (c *Cache) PutAvatar(ctx context.Context, key string, data []byte, source string, gen int) (Image, error)

PutAvatar stores validated bytes only if the cache has not been cleared since gen.

func (*Cache) RememberAvatarMiss added in v0.3.0

func (c *Cache) RememberAvatarMiss(key string, gen int) error

RememberAvatarMiss stores a definitive miss within the discovery's Clear boundary.

func (*Cache) RememberAvatarUnreachable added in v0.3.0

func (c *Cache) RememberAvatarUnreachable(key string, gen int)

RememberAvatarUnreachable notes such a failure, unless the cache was cleared since gen.

func (*Cache) Size

func (c *Cache) Size() int64

Size is the bytes the cache takes on disk.

type Image

type Image struct {
	Data          []byte
	Format        string // png, jpeg, gif, webp; avatar entries also allow svg
	Width, Height int
}

Image is a cached image.

type NotStoredError added in v1.36.0

type NotStoredError struct{ Image Image }

NotStoredError is GetFile's image that downloaded but could not be stored (a full disk, a Clear meanwhile): its bytes are here, with no file.

func (*NotStoredError) Error added in v1.36.0

func (e *NotStoredError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL