Merge branch 'master' into develop
This commit is contained in:
commit
dd7b06c00f
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
- Updated the uploaded filename normalization to take double extensions in consideration ([#4824](https://github.com/pocketbase/pocketbase/issues/4824))
|
- Updated the uploaded filename normalization to take double extensions in consideration ([#4824](https://github.com/pocketbase/pocketbase/issues/4824))
|
||||||
|
|
||||||
|
- Added collections schema cache to help speed up the common List and View requests execution with ~25%.
|
||||||
|
_This was extracted from the ongoing work on [#4355](https://github.com/pocketbase/pocketbase/discussions/4355) and there are many other small optimizations already implemented but they will have to wait for the refactoring to be finalized._
|
||||||
|
|
||||||
|
|
||||||
## v0.22.9
|
## v0.22.9
|
||||||
|
|
||||||
|
|
|
@ -261,7 +261,7 @@ func LoadCollectionContext(app core.App, optCollectionTypes ...string) echo.Midd
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
if param := c.PathParam("collection"); param != "" {
|
if param := c.PathParam("collection"); param != "" {
|
||||||
collection, err := app.Dao().FindCollectionByNameOrId(param)
|
collection, err := core.FindCachedCollectionByNameOrId(app, param)
|
||||||
if err != nil || collection == nil {
|
if err != nil || collection == nil {
|
||||||
return NewNotFoundError("", err)
|
return NewNotFoundError("", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1270,6 +1270,7 @@ func TestRecordCrudCreate(t *testing.T) {
|
||||||
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
||||||
t.Fatalf("failed to update demo3 collection create rule: %v", err)
|
t.Fatalf("failed to update demo3 collection create rule: %v", err)
|
||||||
}
|
}
|
||||||
|
core.ReloadCachedCollections(app)
|
||||||
},
|
},
|
||||||
ExpectedStatus: 400,
|
ExpectedStatus: 400,
|
||||||
ExpectedContent: []string{`"data":{}`},
|
ExpectedContent: []string{`"data":{}`},
|
||||||
|
@ -1291,6 +1292,7 @@ func TestRecordCrudCreate(t *testing.T) {
|
||||||
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
||||||
t.Fatalf("failed to update demo3 collection create rule: %v", err)
|
t.Fatalf("failed to update demo3 collection create rule: %v", err)
|
||||||
}
|
}
|
||||||
|
core.ReloadCachedCollections(app)
|
||||||
},
|
},
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContent: []string{
|
ExpectedContent: []string{
|
||||||
|
@ -1929,6 +1931,7 @@ func TestRecordCrudUpdate(t *testing.T) {
|
||||||
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
||||||
t.Fatalf("failed to update demo3 collection update rule: %v", err)
|
t.Fatalf("failed to update demo3 collection update rule: %v", err)
|
||||||
}
|
}
|
||||||
|
core.ReloadCachedCollections(app)
|
||||||
},
|
},
|
||||||
ExpectedStatus: 404,
|
ExpectedStatus: 404,
|
||||||
ExpectedContent: []string{`"data":{}`},
|
ExpectedContent: []string{`"data":{}`},
|
||||||
|
@ -1950,6 +1953,7 @@ func TestRecordCrudUpdate(t *testing.T) {
|
||||||
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
if err := app.Dao().WithoutHooks().SaveCollection(collection); err != nil {
|
||||||
t.Fatalf("failed to update demo3 collection update rule: %v", err)
|
t.Fatalf("failed to update demo3 collection update rule: %v", err)
|
||||||
}
|
}
|
||||||
|
core.ReloadCachedCollections(app)
|
||||||
},
|
},
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContent: []string{
|
ExpectedContent: []string{
|
||||||
|
|
|
@ -1182,6 +1182,8 @@ func (app *BaseApp) registerDefaultHooks() {
|
||||||
if err := app.initAutobackupHooks(); err != nil {
|
if err := app.initAutobackupHooks(); err != nil {
|
||||||
app.Logger().Error("Failed to init auto backup hooks", slog.String("error", err.Error()))
|
app.Logger().Error("Failed to init auto backup hooks", slog.String("error", err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerCachedCollectionsAppHooks(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getLoggerMinLevel returns the logger min level based on the
|
// getLoggerMinLevel returns the logger min level based on the
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------
|
||||||
|
// This is a small optimization ported from the [ongoing refactoring branch](https://github.com/pocketbase/pocketbase/discussions/4355).
|
||||||
|
//
|
||||||
|
// @todo remove after the refactoring is finalized.
|
||||||
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pocketbase/pocketbase/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
const storeCachedCollectionsKey = "@cachedCollectionsContext"
|
||||||
|
|
||||||
|
func registerCachedCollectionsAppHooks(app App) {
|
||||||
|
collectionsChangeFunc := func(e *ModelEvent) error {
|
||||||
|
if _, ok := e.Model.(*models.Collection); !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = ReloadCachedCollections(app)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
app.OnModelAfterCreate().Add(collectionsChangeFunc)
|
||||||
|
app.OnModelAfterUpdate().Add(collectionsChangeFunc)
|
||||||
|
app.OnModelAfterDelete().Add(collectionsChangeFunc)
|
||||||
|
app.OnBeforeServe().Add(func(e *ServeEvent) error {
|
||||||
|
_ = ReloadCachedCollections(e.App)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReloadCachedCollections(app App) error {
|
||||||
|
collections := []*models.Collection{}
|
||||||
|
|
||||||
|
err := app.Dao().CollectionQuery().All(&collections)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
app.Store().Set(storeCachedCollectionsKey, collections)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindCachedCollectionByNameOrId(app App, nameOrId string) (*models.Collection, error) {
|
||||||
|
// retrieve from the app cache
|
||||||
|
// ---
|
||||||
|
collections, _ := app.Store().Get(storeCachedCollectionsKey).([]*models.Collection)
|
||||||
|
for _, c := range collections {
|
||||||
|
if strings.EqualFold(c.Name, nameOrId) || c.Id == nameOrId {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// retrieve from the database
|
||||||
|
// ---
|
||||||
|
found, err := app.Dao().FindCollectionByNameOrId(nameOrId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ReloadCachedCollections(app)
|
||||||
|
if err != nil {
|
||||||
|
app.Logger().Warn("Failed to reload collections cache", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return found, nil
|
||||||
|
}
|
Loading…
Reference in New Issue