在Web应用程序中管理DataCacheFactory对象的最佳方法是什么?

时间:2013-09-05 15:12:14

标签: asp.net appfabric appfabric-cache datacachefactory

它表示每个执行线程实例化DataCacheFactory个对象的单个实例是最佳做法。我正在web asp.net应用程序中实现AppFabric缓存。

1 个答案:

答案 0 :(得分:1)

建议:

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;
    }
}