Skip to content

Commit

Permalink
added temp collections cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Apr 25, 2024
1 parent 2b82c36 commit 370c2c9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

- 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

Expand Down
2 changes: 1 addition & 1 deletion apis/middlewares.go
Expand Up @@ -261,7 +261,7 @@ func LoadCollectionContext(app core.App, optCollectionTypes ...string) echo.Midd
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if param := c.PathParam("collection"); param != "" {
collection, err := app.Dao().FindCollectionByNameOrId(param)
collection, err := core.FindCachedCollectionByNameOrId(app, param)
if err != nil || collection == nil {
return NewNotFoundError("", err)
}
Expand Down
2 changes: 2 additions & 0 deletions core/base.go
Expand Up @@ -1182,6 +1182,8 @@ func (app *BaseApp) registerDefaultHooks() {
if err := app.initAutobackupHooks(); err != nil {
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
Expand Down
72 changes: 72 additions & 0 deletions core/collections_cache.go
@@ -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) {
reloadFunc := func(e *ModelEvent) error {
if _, ok := e.Model.(*models.Collection); !ok {
return nil // not a collection
}

_ = ReloadCachedCollections(app)

return nil
}
app.OnModelAfterCreate().Add(reloadFunc)
app.OnModelAfterUpdate().Add(reloadFunc)
app.OnModelAfterDelete().Add(reloadFunc)
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
}

0 comments on commit 370c2c9

Please sign in to comment.