Purge cache on publish/unpublish event

Hi guys,

In our system, the HST ( website ) is serving behind a caching server ( named Fastly ). We have a requirement that when editor publish/unpublish a document then system need to automatically purge its related URL.

I have used Event Bus https://documentation.bloomreach.com/14/library/concepts/hippo-services/event-bus.html to subscribe to the publish/unpublish event but I can not find a way to able to calculate the URL from the document path.

I know that we can use the HstLinkCreator to calculate the link but we have no way to get the HstRequestContext or Mount which is necessary to calculate the link.

Any idea would be nice to me ? I thought that a lot of system have such requirement ?

I think you have to do this in the site webapp. You can add a bean to the site (META-INF/…etc) which registers itself on some init method with:

HippoEventListenerRegistry.get().register(this);

You also would have a Subscribe annotated method in there in which you invoke methods of org.hippoecm.hst.content.beans.manager.ObjectBeanManager

Never tried this, but it could work

I missed the mount part: i think you’d have to do this for the mount(s) related to your environment. In that case i guess it does not matter in which webapp you do this. The problem i guess is that you don’t know where a given content item may be used, so unless there is a specific request for it you can’t know which mount.
maybe the following then can help:

final HstModelRegistry service = HippoServiceRegistry.getService(HstModelRegistry.class);
final HstModel model = service.getHstModel(contextPath);
model.getVirtualHosts() ...// .etc calculate links for multiple mounts?

Yes, I think I can iterate on the mounts and conditionally choose the mount in which I want to purge the the cache.

I though the promise of our cache/CDN implementation was to be “seamless” integration and it should take care of this for you. Whatever you are doing, it doesn’t seem to be right.

final HstModelRegistry service = HippoServiceRegistry.getService(HstModelRegistry.class);
final HstModel model = service.getHstModel("/site");
Mount mount = model.getVirtualHosts().getMountByIdentifier("ad60d27f-7862-43cc-af31-bc032b449b08");
HstLink hstLink = model.getHstLinkCreator().create(workflowEvent.subjectPath(), mount);
hstLink.toUrlForm(hstRequestContext, false);

I am able to retrieve the Mount but we faces an other challenging to get the HstRequestContext to be able to extract the final plain URL. Not easy ha ?

Damn. I don’t know how far you want to take this but you could, create your own request context.

HstMutableRequestContext mutableRequestContext = HstServices.getComponentManager().getComponent("org.hippoecm.hst.core.internal.HstRequestContextComponent").create()
//then set what you need to set?

Maybe too hacky

A less hacky way is to implement/use an HST Plain JAX-RS service to generate the HstLinks. The event handler might invoke the jax-rs to either generate urls or process most of the logic.

Regards,

Woonsan

1 Like

Thank you @bcanvural @woonsanko for your idea. Finally I used the same idea of @woonsanko which create and invoke the REST API which make thing easier to be maintained.
The solution has been deployed to LIVE server and working well :slight_smile:

That’s cool! Thanks for the update!

Cheers,

Woonsan