imagecache

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

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) 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) 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) 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.

Jump to

Keyboard shortcuts

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