Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppFabric get named cache object count

Tags:

c#

appfabric

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 currently trying the following:

cache.GetObjectsInRegion("cacheName").Count();

I realize that a region is different than a named cache, but I don't see any other methods that would allow me to achieve this (although I'm sure there are). Any thoughts?

like image 614
downatone Avatar asked Feb 26 '23 19:02

downatone


1 Answers

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.

like image 164
PhilPursglove Avatar answered Mar 08 '23 18:03

PhilPursglove