Skip to content

Commit

Permalink
readd the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Nov 12, 2024
1 parent 12da8a0 commit 7c4933e
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions request_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package frankenphp
import (
"github.com/dunglas/frankenphp/internal/fastabs"
"path/filepath"
"sync"
"sync/atomic"

"go.uber.org/zap"
)

// RequestOption instances allow to configure a FrankenPHP Request.
type RequestOption func(h *FrankenPHPContext) error

var (
documentRootCache sync.Map
documentRootCacheLen atomic.Uint32
)

// WithRequestDocumentRoot sets the root directory of the PHP application.
// if resolveSymlink is true, oath declared as root directory will be resolved
// to its absolute value after the evaluation of any symbolic links.
Expand All @@ -18,20 +25,29 @@ type RequestOption func(h *FrankenPHPContext) error
// symlink is changed without PHP being restarted; enabling this
// directive will set $_SERVER['DOCUMENT_ROOT'] to the real directory path.
func WithRequestDocumentRoot(documentRoot string, resolveSymlink bool) RequestOption {
return func(o *FrankenPHPContext) error {
// make sure file root is absolute
root, err := fastabs.FastAbs(documentRoot)
if err != nil {
return err
return func(o *FrankenPHPContext) (err error) {
v, ok := documentRootCache.Load(documentRoot)
if !ok {
// make sure file root is absolute
v, err = fastabs.FastAbs(documentRoot)
if err != nil {
return err
}

// prevent the cache to grow forever, this is a totally arbitrary value
if documentRootCacheLen.Load() < 1024 {
documentRootCache.LoadOrStore(documentRoot, v)
documentRootCacheLen.Add(1)
}
}

if resolveSymlink {
if root, err = filepath.EvalSymlinks(root); err != nil {
if v, err = filepath.EvalSymlinks(v.(string)); err != nil {
return err
}
}

o.documentRoot = root
o.documentRoot = v.(string)

return nil
}
Expand Down

0 comments on commit 7c4933e

Please sign in to comment.