cache

package
v5.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: AGPL-3.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
	Ping(ctx context.Context) error
}

Cache is our wrapper interface for a cache.

Example (NotFound)
package main

import (
	"context"
	"errors"
	"fmt"

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

func main() {
	ctx := context.Background()
	c, cacheErr := memory.NewInMemoryCache[string](nil, nil, nil)
	if cacheErr != nil {
		panic(cacheErr)
	}

	_, 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/v5/cache/memory"
)

func main() {
	ctx := context.Background()
	c, err := memory.NewInMemoryCache[string](nil, nil, nil)
	if err != nil {
		panic(err)
	}

	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