2012-02-08 182 views
1

在過去,我有這個 http://arens.ws/wordpress/?p=54清除緩存,ObjectCacheManager

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); 
} 

2011年它的與衆不同。有任何想法嗎?

回答

1

它改變了只有一點點:

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); 

我發現這個沒有任何文檔,通過枚舉緩存中找到關鍵的命名方案。可能使用這不是最佳做法,它可能會在下一個版本中再次發生變化?應該有更好的辦法做到這一點...

[更新] 有一個better way

試試這個:

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); 

就像一個魅力...