unarycache

package
v16.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package unarycache allows you to cache responses for unary gRPC messages.

Some gRPC unary message take a considerable amount of compute to build the response. This package will allow users to cache this response.

For the time being it is implemented using a simple in-memory least-recently-used cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a cache that stores values that can be uniquely computed for a specific key.

func New

func New[Key comparable, Value any](maxEntries int, generator Generator[Key, Value]) (*Cache[Key, Value], error)

New creates a new Cache. The cache will hold at maximum `maxEntries`, if more than this many entries are added then the least-recently-used one will be evicted from the cache. If a repository-scoped key does not exist in the cache, then the generator function will be called to compute the value for the given repository and key.

func (*Cache[Key, Value]) GetOrCompute

func (c *Cache[Key, Value]) GetOrCompute(ctx context.Context, repo *localrepo.Repo, key Key) (Value, error)

GetOrCompute either returns a cached value or computes the value for the given repository and key.

type Generator

type Generator[Key comparable, Value any] func(context.Context, *localrepo.Repo, Key) (Value, error)

Generator is a function that computes a cacheable value for a repository based on the given key. The key must uniquely identify the result. A valid choice would for example be an object ID.

Jump to

Keyboard shortcuts

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