0

它说最好的做法是为DataCacheFactory每个执行线程实例化一个对象实例。我正在 Web asp.net 应用程序中实现 AppFabric 缓存。

4

1 回答 1

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;
    }
}
于 2013-09-06T14:59:28.250 回答