简体   繁体   中英

Object Caching using C# in CRM Online

I am using Object Caching in C# to store an EntityCollection in Cache.

Following is the code for caching :

ObjectCache cache = MemoryCache.Default;
const string keyName = "BrandPartners";
EntityCollection result;

if (!cache.Contains(keyName))
{
    result = GetCustomerData(service, firstName, lastName, gender);
    var cacheItemPolicy = new CacheItemPolicy()
    {
        AbsoluteExpiration = DateTime.Now.AddMinutes(5)
    };

    tracingService.Trace("Writing into the cache");
    cache.Add(keyName, result, cacheItemPolicy);
}
else
{
    result = (EntityCollection)cache.Get(keyName);
    tracingService.Trace("Data already found in the cache");
}

So, when I am first creating a record in an entity in an instance in CRM online, I am getting the trace : "Writing into the cache."

But, when I am again creating a record in the same entity, I am again getting the same trace ("Writing into the cache"). But, if I am using caching, I should not get this trace because the keyName of "BrandPartners" already exist in the cache.

So, what am I doing wrong?

Here are some possible reasons why you're not seeing a cached value being found:

  • You're on CRM online, or on-prem and have load balancing in place. The Memory Cache is obviously only for a single process on a single machine.
  • The Workflow activity is registered for multiple workflows/entities, in sandbox mode. In this case, each and every instance of the registration would get a separate sandbox process, and therefore would have separate Memory Caches
  • CRM decided to refresh the plugin sandbox process. This happens from time to time to avoid memory leaks, so this may be the case as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM