12

Is there a suggested method to just clear out all the objects in a DataCache ?

I could use the DataCache.GetObjectsByAllTags method but that required a region, which i cant use since i need to share objects among multiple cache hosts.

PhilPursglove
  • 12,511
  • 5
  • 46
  • 68
np-hard
  • 5,725
  • 6
  • 52
  • 76

4 Answers4

7

There isn't a simple .Clear() on the DataCache object but using the following will clear the cache on the Appfabric hosts:

/* Assumes DataCache as a properly set up Microsoft.ApplicationServer.Caching.Client.DataCache object */

public void Clear()
{
   Parallel.ForEach(DataCache.GetSystemRegions(), region =>
   {
      DataCache.ClearRegion(region);
      var sysRegion = DataCache.GetSystemRegionName(region);
      DataCache.ClearRegion(sysRegion);
   });
}

The problem is, if you have DataCacheLocalCacheProperties set in your configuration you'll still be pulling items from local replica until timeout or notification occurs. I'm still looking for a way to invalidate items in the local replica immediately.

PMontgomery
  • 424
  • 4
  • 17
  • I tried something that in hindsight should have been fairly obvious. Setting the DataCache object to null and then reinitiallizing causes the local replica of the DataCache to be recreated as well. Seems to have desired clear of both AppFabric and local IIS. – PMontgomery Jul 05 '11 at 14:22
  • wouldnt system region list only give me names of regions on my target host ? since region names are local to hosts – np-hard Jul 05 '11 at 21:22
  • @np_hard I am not sure what you are asking here. Do you mean that GetSystemRegions is return the regions on the cache client or only *one* of the nodes in the cache cluster? What I have seen on both nodes in my cache cluster is GetCacheStatistics returns items being removed from cache and cache size dropping to 0 on both nodes. – PMontgomery Jul 06 '11 at 16:33
  • GetSystemRegions() returns a bunch of regions with a name like Default_Region_, which is not the name of any region I'm actually putting data into. – Frank Schwieterman Jan 09 '13 at 23:25
  • Frank, when letting AppFabric do all the management, you should be seeing the regions numbered 0000 through 1023 (if memory serves me correctly). If you're using named regions you lose the whole "distributed/replicated" part of AppFabric as the region will only exist on one cache host. If you *are* using named regions, however, you should still be able to call ClearRegion(string regionName) with the name of the region you are using. Sorry, if I can't be more certain. No long on the project with AppFabric. – PMontgomery Jan 10 '13 at 20:11
3

Have a read of this answer: ASP.Net AppFabric Cache missing Flush/Clear and Count/GetCount methods?

Community
  • 1
  • 1
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
0
$hostname = 'server.lan'
$endpoints = New-Object -TypeName System.Collections.Generic.List[Microsoft.ApplicationServer.Caching.DataCacheServerEndpoint]
$endpoints.Add((New-Object -TypeName Microsoft.ApplicationServer.Caching.DataCacheServerEndpoint -ArgumentList $hostname, 22233))
$cache = ( New-Object -TypeName Microsoft.ApplicationServer.Caching.DataCacheFactory -ArgumentList ( New-Object -TypeName Microsoft.ApplicationServer.Caching.DataCacheFactoryConfiguration -Property @{ Servers = $endpoints } ) ).GetCache('Pricing')
$cache.GetSystemRegions() | %{ $cache.ClearRegion( $_ ) }
Anton Georgiev
  • 610
  • 6
  • 14
0

This is the method to flush the cache that I used. To verify that the cache items were cleared, I ran get-cachestatistics in the command shell.

public void Clear()
{
   Parallel.ForEach(DataCache.GetSystemRegions(), new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, region =>
   {
      DataCache.ClearRegion(region);
   });
}
Liong Ng
  • 21
  • 1