3

I am trying to develop a tool (in Visual Studio 2010, C#) which can read all the items present in an Appfabric cache and store them in a Table. I don't have to use powershell.

First I thought that If I can get all the regions present in the cache, I can make use of the DataCache.GetObjectsInRegion Method to complete my task. But I was not able to get all the region names from the cache as it does not shows the user defined region names but only the default ones, so now I am giving up on this approach.

Can anyone please guide me here, my main goal is to read all the items present in a cache.

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
apudxtr
  • 33
  • 1
  • 6

1 Answers1

5

There is no built-in method to list all items in the cache.

You're correct, it's possible to list all items using GetObjectsInRegion for a named cache. You have to know first all regions names (if used) or call GetSystemRegions to get all (default) system regions. A simple foreach will allow you to list all items. When you put something into the cache without region name, it will be added to a system region.

Here is a basic example

    // Declare array for cache host(s).
    DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
    servers[0] = new DataCacheServerEndpoint("YOURSERVERHERE", 22233);

    // Setup the DataCacheFactory configuration.
    DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();
    factoryConfig.Servers = servers;

    factoryConfig.SecurityProperties = new DataCacheSecurity(DataCacheSecurityMode.None, DataCacheProtectionLevel.None);

    // Create a configured DataCacheFactory object.
    DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig);

    // Get a cache client for the default cache 
    DataCache myCache = mycacheFactory.GetDefaultCache(); //or change to mycacheFactory.GetCache(myNamedCache);

    //inserty dummytest data
    myCache.Put("key1", "myobject1");
    myCache.Put("key2", "myobject2");
    myCache.Put("key3", "myobject3");
    Random random = new Random();

    //list all items in the cache : important part
    foreach (string region in myCache.GetSystemRegions())
    {
        foreach (var kvp in myCache.GetObjectsInRegion(region))
        {
            Console.WriteLine("data item ('{0}','{1}') in region {2} of cache {3}", kvp.Key, kvp.Value.ToString(), region, "default");
        }
    }
HenricF
  • 218
  • 3
  • 19
Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
  • Thank you for your answer but there is one more problem, the GetSystemRegions method is used to determine the default regions for the cache but what about the other non default regions? – apudxtr Jul 09 '13 at 12:03
  • that's the deal : you can't get a specific regions name. This is only possible via Powershell CmdLets on the host (because regions arelimited to a single cache host). You can also try to maintain by yourself a list of all regions names using a wrapper. – Cybermaxs Jul 09 '13 at 12:22
  • only with Powershell Cmdlet Get-CacheRegion. Note : you can invoke it using C# – Cybermaxs Jul 10 '13 at 07:30
  • Is there any way to get the object when the key is known(can be an input), I know about the DataCache.Get(key) method, but the keys are scoped by region so it may return wrong data. I was thinking to store the region names from the Get-CacheRegion cmdlet to a database and then read the DB from my C# code (although I don't know how to do either->see if you can help here also, will save a lot of time).After that I can run a loop and check for the key against each region. If you have any other idea please share and remember this time I know the key also but the problem is I don't know the regoin. – apudxtr Jul 10 '13 at 07:59
  • another complete different way is to implement a Write Behind Provider. items that are in the cache can be periodically written to the backend store using a write-behind provider. This happens asynchronously and on an interval defined by the cache. You could use it do dump keys and regions. see http://msdn.microsoft.com/en-us/library/hh377669.aspx – Cybermaxs Jul 10 '13 at 11:15
  • I tried to implement the storing and reading from a textfile method which was working fine in one of the test servers. But now when I'm trying to run it for the real server I'm getting the following error:"ErrorCode:SubStatus:The connection was terminated, possibly due to server or network problems or serialized Object size is greater than MaxBufferSize on server. Result of the request is unknown." while debugging at the point where in the loop I already have the region name and just entered into the nested loop to get the Objects in region. – apudxtr Jul 12 '13 at 07:52
  • I tried to search a solution for above and tried whatever I understood but still I'm getting the same error. Can you please suggest me something. – apudxtr Jul 12 '13 at 08:04
  • What I think is in "foreach (var kvp in myCache.GetObjectsInRegion(region))" loop, I'm getting the error when it is trying to get the data in var. I got the same error when I tried to get the object using DataCache.Get(key) method. – apudxtr Jul 12 '13 at 08:17
  • another solution is to use a notification callback http://msdn.microsoft.com/en-us/library/hh334285(v=azure.10).aspx – Cybermaxs Jul 16 '13 at 12:33
  • Thanks for all these suggestions, I'll try all of them in the future. :D – apudxtr Jul 24 '13 at 18:15