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
Click to show internal directories.
Click to hide internal directories.