Skip to content

Memory

The memory driver stores files in a map[string][]byte guarded by a sync.RWMutex. Nothing touches the disk. Ideal for unit tests, fixtures, caches, and any place you want a real FileSystem without setup.

  • Tests that need a real filekit backend without temp dirs.
  • Short-lived caches that should disappear with the process.
  • Fuzz harnesses and example programs in docs.
Terminal window
go get github.com/gobeaver/filekit/driver/memory
import (
"context"
"strings"
"github.com/gobeaver/filekit/driver/memory"
)
fs := memory.New()
ctx := context.Background()
_, _ = fs.Write(ctx, "test.txt", strings.NewReader("hi"))

With a size cap (bytes):

fs := memory.New(memory.Config{
MaxSize: 100 * 1024 * 1024, // 100 MiB ceiling
})
type Config struct {
MaxSize int64 // 0 = unlimited
}

When MaxSize is non-zero and a write would push total stored bytes over the limit, the write returns an error rather than evicting anything.

InterfaceImplementedNotes
FileSystemyes
CanCopyyesbyte-slice copy
CanMoveyesrename + delete
CanChecksumyeshashes the in-memory bytes
CanWatchyesnative — callbacks fire synchronously on every matching write/delete
CanSignURLno
CanReadRangeno
ChunkedUploaderno
  • The watcher uses an internal callback list rather than polling, so ActiveChangeCallbacks() returns true. Callbacks run inline on the writing goroutine — keep them fast.
  • CreatedAt is tracked internally and is always populated.
  • Glob patterns for Watch use github.com/gobwas/glob, not stdlib path/filepath.Match, so ** is supported.