Documentation
¶
Overview ¶
Package cache keeps one value read from the database in memory for a short time. Writes in this process invalidate it at once; writes by other processes show up when the time to live has passed.
Reading is lock free: a hit costs two atomic loads. A value loaded while a write invalidated the cache is never kept, because every entry remembers the generation it was loaded in.
Index ¶
Constants ¶
const DefaultTTL = 30 * time.Second
DefaultTTL is how long a value is reused when nothing else is configured.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Value ¶
type Value[T any] struct { // contains filtered or unexported fields }
Value caches the result of a load function. The zero value caches for DefaultTTL; it is safe for concurrent use.
func (*Value[T]) Generation ¶
Generation counts the invalidations (see Invalidate): it changes with every write in this process, and only goes up, so what is made from the value, such as a rendered page, can be kept with the generation it was made at and made again once it moved on. Values loaded again when the time to live has passed do not change it.
func (*Value[T]) Get ¶
Get returns the cached value, or loads and keeps a new one. Inside a transaction it always loads and keeps nothing: the transaction must see its own changes, which are not committed and so not in the cache, and others must not see them before the commit.
func (*Value[T]) Invalidate ¶
func (v *Value[T]) Invalidate()
Invalidate drops the cached value; call it after every committed write.