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 BatchCache ¶
type BatchCache[T any] interface { Cache[T] // GetMany fetches multiple keys in as few round trips as possible. // Missing keys are omitted from the returned map, so a key's absence // from the result is a cache miss. GetMany(ctx context.Context, keys []string) (map[string]*T, error) // SetMany stores multiple values at once, each with the cache's // configured expiration. SetMany(ctx context.Context, items map[string]*T) error }
BatchCache is a Cache that also supports batched reads and writes. Not every Cache implementation supports batching, so callers should obtain a BatchCache via a type assertion on a Cache value.
Example ¶
package main
import (
"context"
"fmt"
"github.com/primandproper/platform-go/cache"
"github.com/primandproper/platform-go/cache/memory"
)
func main() {
ctx := context.Background()
c, err := memory.NewInMemoryCache[string](nil, nil, nil)
if err != nil {
panic(err)
}
// Not every Cache supports batching, so obtain a BatchCache by asserting.
bc, ok := c.(cache.BatchCache[string])
if !ok {
panic("cache does not support batching")
}
one, two := "one", "two"
if err = bc.SetMany(ctx, map[string]*string{"k1": &one, "k2": &two}); err != nil {
panic(err)
}
// Missing keys are simply absent from the result.
results, err := bc.GetMany(ctx, []string{"k1", "k2", "missing"})
if err != nil {
panic(err)
}
fmt.Println(len(results))
fmt.Println(*results["k1"])
}
Output: 2 one
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/primandproper/platform-go/cache"
"github.com/primandproper/platform-go/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/primandproper/platform-go/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 |
|---|---|
|
Package mock provides moq-generated mock implementations of interfaces in the cache package.
|
Package mock provides moq-generated mock implementations of interfaces in the cache package. |
|
slots
Package slots produces Redis Cluster keys whose slots are pre-planned to distribute evenly across the cluster's nodes.
|
Package slots produces Redis Cluster keys whose slots are pre-planned to distribute evenly across the cluster's nodes. |
Click to show internal directories.
Click to hide internal directories.