lib/fs: Cache user lookups (#8496)

This commit is contained in:
Jakob Borg
2022-08-12 07:48:00 +02:00
committed by GitHub
parent 06273875ae
commit eb81f7400c
7 changed files with 85 additions and 16 deletions
+10 -2
View File
@@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"time"
@@ -46,8 +47,13 @@ type BasicFilesystem struct {
root string
junctionsAsDirs bool
options []Option
userCache *userCache
groupCache *groupCache
}
type userCache = valueCache[string, *user.User]
type groupCache = valueCache[string, *user.Group]
func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem {
if root == "" {
root = "." // Otherwise "" becomes "/" below
@@ -84,8 +90,10 @@ func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem {
}
fs := &BasicFilesystem{
root: root,
options: opts,
root: root,
options: opts,
userCache: newValueCache(time.Hour, user.LookupId),
groupCache: newValueCache(time.Hour, user.LookupGroupId),
}
for _, opt := range opts {
opt.apply(fs)