Developer forum

Forum » Development » Clear cache for all orders

Clear cache for all orders

Mario Santos Dynamicweb Employee
Mario Santos
Reply

Hi,

The method Dynamicweb.Ecommerce.Services.Orders.ClearCache() clears cache based on the alternative key caches (AutoId and Secret). This helps if we get orders by AutoId, but not if we get them by OrderId, which is the most common for us.
How can we clear cache for all orders ids cache keys? Should OrderId be included in the alternative key caches?

BR, Mario

 


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Mario,

The ClearCache methods will clear the cache for all of the different keys (order id, autoid, secret). The order id is the primary cache key.

Services.Orders.ClearCache(); // Clear cache for all orders
Services.Orders.ClearCache("ORDER1"); // Clear cache for a single order by order id
Services.Orders.ClearCache(new[] { "ORDER1", "ORDER2", "ORDER3" }); // Clear cache for multiple orders by order id

You only have to call these methods if you make changes to order data directly in the database.

If this doesn't work for you then please share some more information - DW version and your code.

Best regards,
Morten

 
Mario Santos Dynamicweb Employee
Mario Santos
Reply

Hi Morten,

DW version is 9.10.8, code is part of a scheduled task addin that after import orders from ERP does some extra actions to the record in the DB.

We were doing:

Dynamicweb.Ecommerce.Services.Orders.ClearCache();
foreach (var orderId in _ordersToFollowFlow)
{
 var order = Dynamicweb.Ecommerce.Services.Orders.GetById(orderId);
 // make the necessary changes
 Dynamicweb.Ecommerce.Services.Orders.Save(order);
}


We realized that the order object was not matching the DB (there were some differences in the orderlines collection).
Only after we moved the Dynamicweb.Ecommerce.Services.Orders.ClearCache() to inside the foreach and added the orderId as a parameter we started to see the correct object.
The OrderCache set up in OrderService.cs only refers to AutoId and Secret that's why I mentioned only these two as alternative cache keys. How is the OrderId set as the primary one?

BR, Mario

 
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Hi Morten,

I just uncovered possibly a 2nd entity with the same problem. That's: Common.Application.KillProductCategories(). 

Side note, as I type this, I see 2 things that I'm uncertain of.

  1. Common.Application.KillProductCategories may be deprecated, and it should use Ecommerce.Services.ProductCategories instead. But I believe it uses the same code, so either should work.
  2. I really want to clear ProductCategoryFieldValues, however, they are marked as internal in the platform (internal static ProductCategoryFieldGroupValueService), so I don't have access to them directly. So, I was assuming that I could call ClearCache() on ProductCategories and it would take care of the values too. If that's a wrong assumption and there is a correct way to clear ProductCategoryFieldValues, let me know if I'm off track.

Back to the issue ...

That doesn't seem to clear the cache correctly. I believe it's related to this line of code in ServiceCache.cs

foreach (var alternativeCache in AlternativeKeyCaches.Values)
                        alternativeCache.ClearCache();

I believe that AlternativeKeyCaches doesn't account for all of the cache (as Mário pointed out above). It's just one key. So, when it does the lookup to determine everything to clear, it doesn't get the full list, and therefore the clear doesn't clear everything.

One more bit of evidence of this being a problem is that the Cache tool in the backend always shows 0 items, even for well trafficed sites that we're sure have items in cache:

 

and

 

Thanks!

Scott

 
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Bump.

Any thoughts on this? This is impacting one of our customers so they are pressing for a solution. 

Thanks!

 
Nicolai Pedersen
Reply

The categories and their values are 2 different things, so clearing the categories (definitions) the field values are not cleared and should not be reset.

The ProductCategoryFieldGroupValueService it self is public - its instance on Services.ProductCategoryFieldGroupValueService is internal

You can make an instance and call clear cache:

var pgfService = new Ecommerce.Products.Categories.ProductCategoryFieldGroupValueService();
pgfService.ClearCache();

Common.Application.* should be considered deprecated and should not be used at all.

BR Nicolai

 
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Hi Nicolai,

That sounds promising. I'll try it out!

(I believe there is a side issue with the caching service in the backend always showing 0 on a couple of the options and not letting us clear cache from the backend, but that's a less pressing issue)

Scott

 

 

You must be logged in to post in the forum