Documentation
¶
Overview ¶
Package disk provides a persistent bbolt-backed cache store.
Index ¶
- Variables
- type Option
- type Store
- func (s *Store[K, V]) Close() error
- func (s *Store[K, V]) DeletePrefix(ctx context.Context, namespace string, prefix []byte) (int, error)
- func (s *Store[K, V]) Get(ctx context.Context, namespace string, key K) (cache.Entry[V], bool, error)
- func (s *Store[K, V]) GetMany(ctx context.Context, namespace string, keys []K) ([]cache.Entry[V], []bool, error)
- func (s *Store[K, V]) Invalidate(ctx context.Context, namespace string, key K) error
- func (s *Store[K, V]) InvalidateAll(ctx context.Context, namespace string) error
- func (s *Store[K, V]) InvalidateMany(ctx context.Context, namespace string, keys []K) error
- func (s *Store[K, V]) ScanPrefix(ctx context.Context, namespace string, prefix []byte, ...) error
- func (s *Store[K, V]) Set(ctx context.Context, namespace string, key K, entry cache.Entry[V]) error
- func (s *Store[K, V]) SetMany(ctx context.Context, namespace string, keys []K, entries []cache.Entry[V]) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmptyPath = errors.New("cache disk: path must not be empty") ErrNilCodec = errors.New("cache disk: codec must not be nil") ErrNilScanFunc = errors.New("cache disk: scan function must not be nil") ErrOrderedKeysRequired = errors.New("cache disk: prefix operations require WithOrderedKeys") )
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[K comparable, V any] func(*config[K, V]) error
Option configures disk serialization.
func WithKeyCodec ¶
func WithKeyCodec[K comparable, V any](codec cache.Codec[K]) Option[K, V]
WithKeyCodec overrides automatic key serialization.
func WithOrderedKeys ¶
func WithOrderedKeys[K comparable, V any]() Option[K, V]
WithOrderedKeys stores the key codec's bytes verbatim instead of their SHA-256 digest. bbolt then keeps entries in encoded-key byte order, which enables ScanPrefix and DeletePrefix and skips per-key hashing.
Ordered keys are a separate on-disk layout: entries written without this option are not visible with it and vice versa, so a file must always be opened with the same choice. Encoded keys must stay below bbolt's maximum key size of 32 KiB; the store adds one tag byte.
func WithValueCodec ¶
func WithValueCodec[K comparable, V any](codec cache.Codec[V]) Option[K, V]
WithValueCodec overrides automatic value serialization.
type Store ¶
type Store[K comparable, V any] struct { // contains filtered or unexported fields }
Store persists entries in a bbolt database owned by the store.
Store implements cache.BatchStore. Each batch call runs in a single bbolt transaction, so a batch is applied atomically and costs one commit.
func (*Store[K, V]) DeletePrefix ¶
func (s *Store[K, V]) DeletePrefix(ctx context.Context, namespace string, prefix []byte) (int, error)
DeletePrefix removes every record in namespace whose encoded key starts with prefix in one atomic write transaction. It returns the number of records removed, including logically expired ones. Prefix semantics match ScanPrefix; an empty prefix removes every entry in the namespace. The store must be opened with WithOrderedKeys; otherwise DeletePrefix returns ErrOrderedKeysRequired.
func (*Store[K, V]) Get ¶
func (s *Store[K, V]) Get(ctx context.Context, namespace string, key K) (cache.Entry[V], bool, error)
Get returns the live entry stored under key in namespace and whether one is present. Expired entries and entries written before the namespace's last InvalidateAll are reported as absent.
func (*Store[K, V]) GetMany ¶
func (s *Store[K, V]) GetMany(ctx context.Context, namespace string, keys []K) ([]cache.Entry[V], []bool, error)
GetMany looks up keys in one read transaction. Results are aligned with keys.
func (*Store[K, V]) Invalidate ¶
Invalidate removes key from namespace. Removing a missing key is not an error.
func (*Store[K, V]) InvalidateAll ¶
InvalidateAll removes every entry in namespace in one write transaction and advances the namespace generation.
func (*Store[K, V]) InvalidateMany ¶
InvalidateMany removes keys in one atomic write transaction.
func (*Store[K, V]) ScanPrefix ¶
func (s *Store[K, V]) ScanPrefix(ctx context.Context, namespace string, prefix []byte, fn func(key K, entry cache.Entry[V]) bool) error
ScanPrefix calls fn for every live entry in namespace whose encoded key starts with prefix, in ascending encoded-key byte order, until fn returns false. Expired entries and entries written before the last InvalidateAll are skipped. An empty prefix visits the whole namespace.
prefix is matched against the bytes produced by the store's key codec, not against K itself. With the default codec a string key encodes to its raw bytes and a []byte key to itself, so prefix is a plain string or byte prefix; integers encode as decimal text (lexicographic, not numeric, order) and composite keys as JSON. Supply a custom key codec when another ordering is required. The store must be opened with WithOrderedKeys; otherwise ScanPrefix returns ErrOrderedKeysRequired.
The scan runs inside one bbolt read transaction that stays open while fn executes, so fn must not write to the same store. Key and value codecs must not retain the byte slices they decode.
Example ¶
package main
import (
"context"
"fmt"
"os"
"path/filepath"
cache "github.com/justusschock/cache"
"github.com/justusschock/cache/backend/disk"
)
func main() {
dir, err := os.MkdirTemp("", "go-cache-example")
if err != nil {
panic(err)
}
defer func() { _ = os.RemoveAll(dir) }()
store, err := disk.Open(filepath.Join(dir, "hashes.db"), disk.WithOrderedKeys[string, string]())
if err != nil {
panic(err)
}
defer func() { _ = store.Close() }()
ctx := context.Background()
paths := []string{"src/b.go", "docs/readme.md", "src/a.go"}
entries := []cache.Entry[string]{
{Value: "hash-b", Found: true},
{Value: "hash-readme", Found: true},
{Value: "hash-a", Found: true},
}
if err := store.SetMany(ctx, "file-hashes", paths, entries); err != nil {
panic(err)
}
err = store.ScanPrefix(ctx, "file-hashes", []byte("src/"), func(path string, entry cache.Entry[string]) bool {
fmt.Println(path, entry.Value)
return true
})
if err != nil {
panic(err)
}
removed, err := store.DeletePrefix(ctx, "file-hashes", []byte("src/"))
if err != nil {
panic(err)
}
fmt.Println("removed", removed)
}
Output: src/a.go hash-a src/b.go hash-b removed 2