ASP.Net AppFabric Cache missing Flush/Clear and Count/GetCount methods?
Asked Answered
A

2

7

I am trying to convert a solution using EntLib into using AppFabric caching. By help of a few extension methods this is a fairly pain-free process.

Extension methods I use:

public static bool Contains(this DataCache dataCache, string key)
{
    return dataCache.Get(key) != null;
}

public static object GetData(this DataCache dataCache, string key)
{
    return dataCache.Get(key);
}

But there are two features of EntLib I find difficult to convert. Namely "Count" (counting number of keys in cache) and "Flush" (removing all data from cache). Both could be solved if I could iterate the keys in cache.

There is a method called ClearRegion(string region), but that required me to specify a region name on all Get/Put/Add-methods I use, which would require some manual error-prone work.

Is there any way to get a list of keys in cache?
Is there a default region name I can use?
How can I flush the cache when I haven't used a region name?

Acidimeter answered 14/1, 2011 at 13:32 Comment(1)
The code allowed me to replace ".Count == 0" with the .Contains() method above.Acidimeter
T
10

See my previous answer for my speculation as to how the cache works internally when you don't specify a region, and how you can get the count of objects that aren't in a named region.

We can build a Flush method using the same technique:

public void Flush (this DataCache cache)
{
    foreach (string regionName in cache.GetSystemRegions()) 
    {     
        cache.ClearRegion(regionName) 
    } 
}

As I said there, I think named regions are probably the way to go - it seems to me that using them solves more problems than it creates.

Treen answered 14/1, 2011 at 13:57 Comment(2)
Thanks. It seems it has created a whole set of regions automatically. Default_Region_0000 through Default_Region_1023. Clearing all shows that they are empty (Get-CacheStatistics).Acidimeter
If you use regions, you'll lose the distributed part of the cache as regions are created on only one cache host. I think, in general you'll want to stay away from regions unless you have a very specific reason to use them (i.e. tags).Nolly
N
0

If anyone will have problems in future (like me) - here is the full code for clearing cache.

private static DataCacheFactory _factory;
        private const String serverName = "<machineName>";
        private const String cacheName = "<cacheName>";

        static void Main(string[] args)
        {
            Dictionary<String, Int32> cacheHostsAndPorts = new Dictionary<String, Int32> { { serverName, 22233 } };
            Initialize(cacheHostsAndPorts);
            DataCache cache = _factory.GetCache(cacheName);
            FlushCache(cache); 
            Console.WriteLine("Done");
            Console.ReadLine();
        }

        private static void FlushCache(DataCache cache)
        {
            foreach (string regionName in cache.GetSystemRegions())
            {
                cache.ClearRegion(regionName);
            }
        }

        public static void Initialize(Dictionary<String, Int32> cacheHostsAndPorts)
        {
            var factoryConfig = new DataCacheFactoryConfiguration
            {
                Servers = cacheHostsAndPorts.Select(cacheEndpoint => new DataCacheServerEndpoint(cacheEndpoint.Key, cacheEndpoint.Value))
            };

            _factory = new DataCacheFactory(factoryConfig);
        }
Negus answered 28/8, 2014 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.