How to subscribe to an event in a plugin?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Can anybody give an example of event usage, I need to start a process when item is added or removed to the cart.

Thanks!
12 years ago
Just create a class that implements IConsumer<T>. It will get picked up by the Container builder when the application starts (if the assembly is in the app domain :)

Here's an example of a class that listens for various events for product, product variant, discount (I cache a lot of stuff in Nop and I use events to clear the cache when entities are updated in the backend).


public class ModelCacheEventConsumer : IConsumer<EntityUpdated<Discount>>,
        IConsumer<EntityUpdated<ProductVariant>>,
        IConsumer<EntityUpdated<Category>>,
        IConsumer<EntityDeleted<ProductVariant>>
    {...


It works very well.
12 years ago
Thanks!
I added this in a plugin and event are raising perfectly!

public class GiftsPlugin : BasePlugin, IConsumer<EntityUpdated<ShoppingCartItem>>, IConsumer<EntityInserted<ShoppingCartItem>>, IConsumer<EntityDeleted<ShoppingCartItem>>
{

    public void HandleEvent(EntityUpdated<ShoppingCartItem> eventMessage)
    {
        //using eventMessage.Entity
    }

    public void HandleEvent(EntityInserted<ShoppingCartItem> eventMessage)
    {
        //using eventMessage.Entity
    }

    public void HandleEvent(EntityDeleted<ShoppingCartItem> eventMessage)
    {
        //using eventMessage.Entity
    }

}
12 years ago
asoares wrote:
...I cache a lot of stuff in Nop and I use events to clear the cache when entities are updated in the backend...

Great to hear that you did it. This task is also planned for the upcoming version 2.30 (presentation layer model caching). It'll be greatly appreciated if you could share some parts of your implementation here (ModelCacheEventConsumer, an usage in some controller). It'll save some time for me when I start working on it.
12 years ago
Just another question : If I am working with a webfarm, or in the cloud, or just I have many pools because of the hight traffic, is this event system still working?
12 years ago
a.m. wrote:
...I cache a lot of stuff in Nop and I use events to clear the cache when entities are updated in the backend...
Great to hear that you did it. This task is also planned for the upcoming version 2.30 (presentation layer model caching). It'll be greatly appreciated if you could share some parts of your implementation here (ModelCacheEventConsumer, an usage in some controller). It'll save some time for me when I start working on it.


Hi,

Sure thing, I'll try to do that asap. Since I created a clone of the code repository, can I create a push request for some files or should I send you a zip, or just publish it here?

I also implemented a CustomCacheAttribute that allows me to flush targeted controller's actions output when needed. So, using the same event system, I can flush both from the nop_static_cache ICacheManager and from my CustomCacheAttribute (which actually don't use the ICacheManager but could I suppose...!) So everything that's called as an action is cached using the OutputCacheAttribute and everything that's not is cached using the ModelCache. When there are prices in the data, I cache different versions per language-tax exemption-currency. Otherwise, its just language.

One last thing I did to improve the speed: everywhere except on a product detail page and on the cart/checkout, I compute what I call "potential" discount: I just check the discount validity without checking the requirements and only if it does not require a coupon. When the customer passed is null, I return true when the previous conditions are met. This way I can cache the whole ProductModel (from CatalogController.PrepareProductModelOverview) and if there is a discount and that it has requirements, I just show a "*" next to the deal with a fancybox link that shows a topic with a system name of "discount<id>" that explains in plain english (or whatever language :) what is required to get this deal. Not perfect but it fits our needs.

Being into it, I also added a "Free shipping" option to the list of shipping options when free shipping is available. Just so people still have the choice to pay to get a faster shipping.
12 years ago
nicolas.muniere wrote:
Just another question : If I am working with a webfarm, or in the cloud, or just I have many pools because of the hight traffic, is this event system still working?


I don't think the current implementation will work in those scenarios. I think a messaging queueing system would be requiredt to have multiple brokers talk together, something like http://www.rabbitmq.com/. I don't mean to answer for the team though :)
12 years ago
asoares wrote:
Sure thing, I'll try to do that asap. Since I created a clone of the code repository, can I create a push request for some files or should I send you a zip, or just publish it here?

Thanks a lot! It would be great if you could upload the archived solution (Mercurial repository if possible) to a file sharing hosting.
12 years ago
asoares wrote:

I also implemented a CustomCacheAttribute that allows me to flush targeted controller's actions output when needed. So, using the same event system, I can flush both from the nop_static_cache ICacheManager and from my CustomCacheAttribute (which actually don't use the ICacheManager but could I suppose...!) So everything that's called as an action is cached using the OutputCacheAttribute and everything that's not is cached using the ModelCache. When there are ...


Hi, could you develop further on this a little bit? :)

I'm finishing a plugin/widget that displays a dynamic popup category navigation menu using pure CSS, and I've been looking into caching to avoid re-generating the Category tree in every request.

I tried to use MVC 3 OutputCaching in my plugin controller, but it's not working as expected (not varying by 'slug', which is in this case the currentCategoryId -perhaps because the "main" action is invoked by the WidgetManager and is only sending the widgetId parameter, and the action that actually builds the menu is a child action called from the former...) so I looked into using nop's builtin MemoryCache. It works fine and I can invalidate it implementing IConsumer<X>.

The problem is, to mark the 'active' category, I still have to get the current category id and find it in my cached-tree to mark it as Active (and its parent categories optionally).

That's the reason I'm interested in some automation here, if some OutputCache attribute in my plugin controller can handle the different parameters, I think I can live with 1 cached tree for each category in the system.

Any hints/advice would be appreciated :)
12 years ago
My class is heavily based on http://thenullreference.com/blog/fixing-the-asp-net-mvc-3-outputcacheattribute-for-partial-views-to-honor-some-web-config-settings/. I added a map from full action namespace to cache keys. This map is static and I have a static method that I call that clears all keys for a given action.

I think the OutputCache attribute provided in asp.net mvc supports the VaryByParam if you give it a "*" it should create versions of the cache for each parameters combination.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.