1

In the old days, I had this http://arens.ws/wordpress/?p=54

public static void ClearCache(string entityName)
{
  const string format = "adxdependency:crm:entity:{0}";
  var dependency = string.Format(format, entityName).ToLower();

  var cache = Microsoft.Xrm.Client.Caching.CacheManager.GetBaseCache();
  cache.Remove(dependency);
}

In 2011 it's different. Any ideas?

skaffman
  • 398,947
  • 96
  • 818
  • 769
DevilCode
  • 37
  • 2
  • 10

1 Answers1

1

It has changed only a little bit:

ObjectCache cache = Microsoft.Xrm.Client.Caching.ObjectCacheManager
    .GetInstance("Xrm");
string cachekey = String.Format("xrm:dependency:entity:{0}:id={1:D}", 
    entity.LogicalName, entity.Id);
cache.Remove(cachekey);

I found no documentation for this, found the key naming scheme by enumerating the cache. Probably using this is not a best practice, it could change again in the next version? There should be a better way to do this...

[Update] There is a better way.

Try this:

var serviceContext = (Get an OrganizationServiceContext);

var serviceContainer = serviceContext as 
                           OrganizationServiceContainer;
var cachedOrgService = serviceContainer.Service as 
                           CachedOrganizationService;
var orgServiceCache = cachedOrgService.Cache as 
                           IOrganizationServiceCache;
var entity = (Get the entity that was updated);

orgServiceCache.Remove(entity.LogicalName, entity.Id);

Works like a charm...

Community
  • 1
  • 1
Maarten
  • 1,941
  • 1
  • 15
  • 14