How can I plug into the cache invalidation system after a product gets promoted and/or approved?
Whenever there is a save to a product or order, a list of cache invalidation producers are called. The Broadleaf framework has two events that get registered. One for sandbox updates, SolrSandBoxProductChangeProducer
(this is meant to allow searching while previewing on site) and one for approvals/deployments, SolrProductInvalidationProducer
. These create events that are then consumed by the SolrDocInvalidationEventConsumer
to do the necessary reindexing.
Let's say that you want to plug into this event system in order to invalidate Solr documents attached to categories. While the logic to actually do the document invalidation is beyond the scope of this article, you can setup the event system that would trigger said logic with the following approach.
In this case, you'd want to create your own MySolrCategoryInvalidationProducer
and MySolrCategoryInvalidationConsumer
. You would setup the Producer in the same way the BLC producer is setup by first defining the bean then adding it to the list. In doing so it will trigger when a deployment happens.
<bean id="mySolrCategoryInvalidationProducer" class="path.to.producer.MySolrCategoryInvalidationProducer" /> <bean id="myAdditionalCacheInvalidationProducers" class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="sourceList"> <list> <ref bean="mySolrCategoryInvalidationProducer" /> </list> </property> </bean> <bean class="org.broadleafcommerce.common.extensibility.context.merge.LateStageMergeBeanPostProcessor"> <property name="collectionRef" value="myAdditionalCacheInvalidationProducers" /> <property name="targetRef" value="blAdditionalCacheInvalidationProducers" /> </bean>
At this point you can then create the consumer to do whatever it needs so it can appropriately index/reindex Solr.