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

前端 未结 2 1315
北海茫月
北海茫月 2021-01-05 08:06

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

相关标签:
2条回答
  • 2021-01-05 08:44

    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.

    0 讨论(0)
  • 2021-01-05 09:07

    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);
            }
    
    0 讨论(0)
提交回复
热议问题