I have a named cache and simply want to return (programmatically) its object count.
I have a DataCache object: cache = factory.GetCache(\"cacheName\");
I\'m
This isn't as straightforward as you might have expected - but it is do-able.
When an object is added to an AppFabric cache, it goes into a region, whether you specify the region or not. When a cache is created, a set of default regions get created (1024 of them on my test rig, Default_Region_0000
, Default_Region_0001
and so on), and as items are added to the cache there is, I assume, some algorithm that determines which of the regions they go into.
So, to find the total number of objects in the cache outside of named regions, you need to run GetObjectsInRegion for every one of the default regions:
int totalItemCount = 0;
foreach (string regionName in cache.GetSystemRegions())
{
totalItemCount += cache.GetObjectsInRegion(regionName).Count();
}
Given the complexity of these 1024 regions, I think you could make a convincing case that every object should be in a named region: there's effectively no cost for doing so, and a potential benefit in that it becomes easier to see how many objects are in the cache. If this is a common requirement for you, you could also think about making this an extension method on the DataCache class.