0

It says it is the best practice to instantiate one single instance of DataCacheFactory object per execution thread. I am implementing AppFabric caching in a web asp.net application.

dexter
  • 7,063
  • 9
  • 54
  • 71

1 Answers1

1

Suggestion:

public class AppFabricCacheWrapper
{
    public static DataCache GetCache(string cacheName)
    {
        var cacheFactory = GetDataCacheFactory();
        return cacheFactory.GetCache(cacheName);
    }

    private static DataCacheFactory GetDataCacheFactory()
    {
        if (HttpContext.Current.Items["DataCacheFactory"] == null)
        {
            CreateFactoryInstance();
        }

        return (DataCacheFactory)HttpContext.Current.Items["DataCacheFactory"];
    }

    private static void CreateFactoryInstance()
    {
        var config = new DataCacheFactoryConfiguration
        {
            Servers = GetServerNames(),
        };

        HttpContext.Current.Items["DataCacheFactory"] = new DataCacheFactory(config);
    }

    private static IEnumerable<DataCacheServerEndpoint> GetServerNames()
    {
        var namesList = new List<DataCacheServerEndpoint>
            {
                new DataCacheServerEndpoint("SERVER1", 22233),
                new DataCacheServerEndpoint("SERVER2", 22233)
            };

        return namesList;
    }
}
Craig Wilcox
  • 1,577
  • 1
  • 12
  • 23