如何阅读Appfabric缓存中的所有项目:

时间:2013-07-09 10:45:33

标签: appfabric appfabric-cache azure-appfabric

我正在尝试开发一个工具(在Visual Studio 2010中,C#),它可以读取Appfabric缓存中存在的所有项目并将它们存储在表格中。我不必使用powershell。

首先,我认为如果我可以获取缓存中存在的所有区域,我可以使用DataCache.GetObjectsInRegion方法来完成我的任务。但我无法从缓存中获取所有区域名称,因为它没有显示用户定义的区域名称,只显示默认区域名称,所以现在我放弃了这种方法。

任何人都可以在这里指导我,我的主要目标是阅读缓存中的所有项目。

1 个答案:

答案 0 :(得分:4)

没有内置方法列出缓存中的所有项目。

你是对的,可以使用GetObjectsInRegion为命名缓存列出所有项目。您必须首先了解所有区域名称(如果使用)或调用GetSystemRegions以获取所有(默认)系统区域。一个简单的foreach将允许您列出所有项目。当您将某些内容放入缓存而没有区域名称时,它将被添加到系统区域。

这是一个基本的例子

    // 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");
        }
    }
相关问题