countcache

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package countcache keeps the audience counts that read every contact of a workspace for a short while (review 2026-11 OP-12): the Audience header's counts (about 300 ms on a million contacts) and a segment's count (about a second for two conditions).

Only counts that took MinCost or longer are kept: a quick count is cheaper to run again than to serve stale, so small workspaces always see fresh numbers. An entry lives for TTL. A change this process makes to a workspace's contacts, lists or suppressions drops the workspace's entries at once (Changed, wired to the audit log, which every such change writes); a change another process makes shows within TTL. Concurrent misses for the same entry run one count and share its result.

Index

Constants

View Source
const DefaultMax = 4096

DefaultMax bounds the entries kept, across workspaces.

View Source
const DefaultTTL = time.Minute

DefaultTTL is how long a count is served without being counted again.

View Source
const SlowCount = 250 * time.Millisecond

SlowCount is the MinCost the app uses: a quarter of a second, about what the Audience header's counts take on a million contacts.

Variables

This section is empty.

Functions

func AudienceChange

func AudienceChange(action string) bool

AudienceChange reports whether an audit action changes what the counts count ("contacts.erase", "list.delete", "import.finish"…).

func Get

func Get[T any](ctx context.Context, c *Cache, ws uuid.UUID, name string, load func(context.Context) (T, error)) (val T, at time.Time, err error)

Get returns the workspace's count called name, counting it with load when it isn't kept or is older than the TTL. at is when the value was counted. A nil cache always loads.

func Put

func Put[T any](c *Cache, ws uuid.UUID, name string, val T)

Put keeps a value counted elsewhere (a recount the person asked for).

Types

type Cache

type Cache struct {
	// MinCost is the least time a count must have taken to be kept; zero
	// keeps every count. Set it before the cache is used.
	MinCost time.Duration
	// contains filtered or unexported fields
}

Cache is safe for concurrent use. The zero value isn't: use New.

func New

func New(ttl time.Duration, max int, now func() time.Time) *Cache

New returns a cache; ttl ≤ 0 means DefaultTTL, max ≤ 0 DefaultMax, and a nil now the wall clock.

func (*Cache) Changed

func (c *Cache) Changed(ws uuid.UUID)

Changed drops a workspace's counts: the next read counts again.

func (*Cache) Len

func (c *Cache) Len() int

Len is the number of entries kept (tests and metrics).

Jump to

Keyboard shortcuts

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