cache

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides a generic caching interface with support for multiple backend implementations including Redis and in-memory stores.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type Cache

type Cache[T any] interface {
	Get(ctx context.Context, key string) (*T, error)
	Set(ctx context.Context, key string, value *T) error
	Delete(ctx context.Context, key string) error
}

Cache is our wrapper interface for a cache.

Example (NotFound)
package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v2/cache"
	"github.com/verygoodsoftwarenotvirus/platform/v2/cache/memory"
)

func main() {
	ctx := context.Background()
	c := memory.NewInMemoryCache[string]()

	_, err := c.Get(ctx, "nonexistent")
	fmt.Println(err)
	fmt.Println(errors.Is(err, cache.ErrNotFound))
}
Output:

not found
true
Example (SetAndGet)
package main

import (
	"context"
	"fmt"

	"github.com/verygoodsoftwarenotvirus/platform/v2/cache/memory"
)

func main() {
	ctx := context.Background()
	c := memory.NewInMemoryCache[string]()

	value := "cached-value"
	if err := c.Set(ctx, "my-key", &value); err != nil {
		panic(err)
	}

	result, err := c.Get(ctx, "my-key")
	if err != nil {
		panic(err)
	}

	fmt.Println(*result)
}
Output:

cached-value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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