cache

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsLive added in v0.1.1

func IsLive(expiration time.Time) bool

IsLive is the IsLiveAt shorthand that samples time.Now() once per call.

func IsLiveAt added in v0.1.1

func IsLiveAt(expiration, now time.Time) bool

IsLiveAt reports whether an expiration deadline is still in the future relative to now. It centralises the "no expiration" sentinel handling shared across the vertex cache, the edge cache, and any future expiring store. Two values are treated as "never expires":

  • Go zero time (`time.Time{}`, year 1) — the documented sentinel for "no expiration" used by service.validateExpiration and the proto contract (`Vertex.expiration` / `Edge.expiration` are optional).
  • Unix epoch or earlier (`expiration.Unix() <= 0`) — `(*timestamppb. Timestamp)(nil).AsTime()` returns `time.Unix(0,0).UTC()`, NOT Go zero, so the cache used to silently treat every PutVertex without an expiration as already-expired (issue #250).

Any positive future deadline behaves as before: live until now passes it.

Types

type Cache

type Cache[S comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[S comparable, T any](defaultTTL time.Duration) *Cache[S, T]

func (*Cache[S, T]) Clear

func (c *Cache[S, T]) Clear()

Clear removes every entry. When an OnEvict callback is installed it is invoked once per removed key after c.mu has been released. Keys are reported in unspecified order.

func (*Cache[S, T]) Count

func (c *Cache[S, T]) Count() int

func (*Cache[S, T]) Delete

func (c *Cache[S, T]) Delete(key S) bool

Delete removes the entry for key. It returns true if the key was present (and therefore removed by this call), false otherwise. When the key was present and an OnEvict callback is installed, the callback fires once after c.mu is released.

func (*Cache[S, T]) Flush

func (c *Cache[S, T]) Flush() int

Flush evicts every entry whose TTL has passed. It returns the number of entries removed so callers (e.g. server-side TTL metrics) can record expiration counts without scanning the cache again. When an OnEvict callback is installed it is invoked once per removed key after c.mu has been released.

func (*Cache[S, T]) Get

func (c *Cache[S, T]) Get(key S) (T, bool)

func (*Cache[S, T]) Has

func (c *Cache[S, T]) Has(key S) bool

func (*Cache[S, T]) Put

func (c *Cache[S, T]) Put(key S, value T)

func (*Cache[S, T]) PutWithExpiration

func (c *Cache[S, T]) PutWithExpiration(key S, value T, expiration time.Time)

func (*Cache[S, T]) PutWithTTL

func (c *Cache[S, T]) PutWithTTL(key S, value T, ttl time.Duration)

func (*Cache[S, T]) Range added in v0.1.1

func (c *Cache[S, T]) Range(fn func(key S, value T, expiration time.Time) bool)

Range invokes fn for every live (non-expired) entry. fn returns false to stop iteration early; the boolean return mirrors the iter.Seq2 contract callers may already be wired against. Range takes c.mu.RLock for the duration of the walk, so fn MUST NOT call back into the same Cache (which would acquire c.mu.Lock and deadlock with the reader-priority blockage described in #202). The intended consumer is the replication snapshot path (#184), which iterates under the GraphCache write lock where this constraint is trivially satisfied.

func (*Cache[S, T]) SetOnEvict added in v0.1.1

func (c *Cache[S, T]) SetOnEvict(fn func(S))

SetOnEvict installs (or clears, when nil) a callback invoked once per key removed by Delete, Clear, or Flush. The callback fires after c.mu has been released, so it may re-enter the same Cache without deadlocking. It is invoked inline on the caller's goroutine, so it must not block.

Eviction notifications enable layered caches (e.g. GraphCache wiring a shared vertex-id dictionary) to release per-entry resources without scanning the cache each tick.

func (*Cache[S, T]) Watch

func (c *Cache[S, T]) Watch(ctx context.Context, interval time.Duration)

type LoadingCache

type LoadingCache[S comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewLoadingCache

func NewLoadingCache[S comparable, T any](ctx context.Context, loader function.Loader[S, T], defaultTTL time.Duration) *LoadingCache[S, T]

func (*LoadingCache[S, T]) Get

func (c *LoadingCache[S, T]) Get(key S) (T, bool)

func (*LoadingCache[S, T]) Set

func (c *LoadingCache[S, T]) Set(key S, value T)

func (*LoadingCache[S, T]) SetWithTTL

func (c *LoadingCache[S, T]) SetWithTTL(key S, value T, ttl time.Duration)

Directories

Path Synopsis
example command

Jump to

Keyboard shortcuts

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