feat(16.2-07): purge the host site cache after a category admin update
- classes.SiteCachePurger posts {kind, slug} with a bearer token to
golem15.journal.site_cache.purge_url, 2 s timeout, logged, never fails a save
- PurgeSiteCacheAfterCommit runs it through lagoon.AfterCommit
- Boot builds and publishes the purger from the two config keys
- categories FormAfterUpdate registers the purge; Postgres test proves
one authorized request after the commit
This commit is contained in:
@@ -60,6 +60,17 @@ func (categoriesAdminController) FormBeforeUpdate(ctx context.Context, model any
|
||||
return prepareCategoryWrite(cat)
|
||||
}
|
||||
|
||||
// FormAfterUpdate purges the host site's page caches once the save
|
||||
// transaction commits (G-16.2-2, D-19).
|
||||
func (c categoriesAdminController) FormAfterUpdate(ctx context.Context, model any) error {
|
||||
cat, ok := model.(*models.Category)
|
||||
if !ok || cat == nil {
|
||||
return errors.New("categories admin: form model is not a category")
|
||||
}
|
||||
purgeSiteCache(ctx, c.app, "category", cat.Slug)
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepareCategoryWrite stores blank category content (white space only) as
|
||||
// NULL and accepts any other content it receives. Since Phase 16.2 the
|
||||
// content field is the blogeditor type, whose Decode already stored the HTML
|
||||
@@ -84,5 +95,6 @@ var (
|
||||
_ pact.AdminRecordSource = categoriesAdminController{}
|
||||
_ pact.FormBeforeCreate = categoriesAdminController{}
|
||||
_ pact.FormBeforeUpdate = categoriesAdminController{}
|
||||
_ pact.FormAfterUpdate = categoriesAdminController{}
|
||||
_ cabana.FieldRelationProvider = categoriesAdminController{}
|
||||
)
|
||||
|
||||
32
controllers/site_cache.go
Normal file
32
controllers/site_cache.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.golem15.com/golem15/sm-journal-plugin/classes"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||
)
|
||||
|
||||
// purgeSiteCache registers a site cache purge for one saved entity to run
|
||||
// after the save transaction commits. Without a published
|
||||
// *classes.SiteCachePurger (no purge configured) or outside a save
|
||||
// transaction it does nothing.
|
||||
func purgeSiteCache(ctx context.Context, app func() *backpack.App, kind, slug string) {
|
||||
if app == nil {
|
||||
return
|
||||
}
|
||||
a := app()
|
||||
if a == nil {
|
||||
return
|
||||
}
|
||||
purger, ok := a.Lookup[*classes.SiteCachePurger]()
|
||||
if !ok || purger == nil {
|
||||
return
|
||||
}
|
||||
tx, ok := cabana.TxFromContext(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
classes.PurgeSiteCacheAfterCommit(ctx, tx, purger, kind, slug)
|
||||
}
|
||||
Reference in New Issue
Block a user