Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithCache ¶
func WithCache[T any](c Cache[T], loadFromDatabase func(ctx context.Context, identifier string) (T, bool, error)) func(ctx context.Context, identifier string) (T, bool, error)
withCache builds a pullthrough cache function to wrap a database call. Example: api, found, err := withCache(s.apiCache, s.db.FindApiByKeyAuthId)(ctx, key.KeyAuthId)
Types ¶
type Cache ¶
type Cache[T any] interface { // Get returns the value for the given key. // If the key is not found, found will be false. Get(ctx context.Context, key string) (value T, hit CacheHit) // Sets the value for the given key. Set(ctx context.Context, key string, value T) // Sets the given key to null, indicating that the value does not exist in the origin. SetNull(ctx context.Context, key string) // Removes the key from the cache. Remove(ctx context.Context, key string) // Dump returns a serialized representation of the cache. Dump(ctx context.Context) ([]byte, error) // Restore restores the cache from a serialized representation. Restore(ctx context.Context, data []byte) error // Clear removes all entries from the cache. Clear(ctx context.Context) }
func NewNoopCache ¶
type Config ¶
type Config[T any] struct { // How long the data is considered fresh // Subsequent requests in this time will try to use the cache Fresh time.Duration // Subsequent requests that are not fresh but within the stale time will return cached data but also trigger // fetching from the origin server Stale time.Duration // A handler that will be called to refetch data from the origin when necessary RefreshFromOrigin func(ctx context.Context, identifier string) (data T, ok bool) Logger logging.Logger Metrics metrics.Metrics // Start evicting the least recently used entry when the cache grows to MaxSize MaxSize int Resource string }
type Middleware ¶
Click to show internal directories.
Click to hide internal directories.