简体   繁体   English

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

[英]What is the best way to manage the DataCacheFactory object in a web application?

It says it is the best practice to instantiate one single instance of DataCacheFactory object per execution thread. 它说,最佳实践是每个执行线程实例化一个DataCacheFactory对象的单个实例。 I am implementing AppFabric caching in a web asp.net application. 我正在Web asp.net应用程序中实现AppFabric缓存。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Web应用程序中处理DateTime的最佳方法是什么? - What is the best way to handle DateTime in a Web Application? 更新ASP.NET Web应用程序DLL的最佳方法是什么? - What is the best way to update an ASP.NET web application DLL? 对一个Web应用程序进行多种配置的最佳方法是什么 - What is the best way to have multiple configurations for one web application 从Web应用程序创建锁的最佳方法是什么? - What is the best way to create a lock from a web application? 从Web应用程序中记录某人的最佳方法是什么? - What is the best way to log someone out of a web application? 从ASP.Net Web应用程序中的WCF验证Web用户的最佳方法是什么? - What is the best way to authenticate web users from WCF coming from ASP.Net web application? 管理一个人的会话变量的最佳方法是什么? - What is the best way to manage one's session variables? 在ASP.NET网站的ActiveDirectory中管理角色的最佳方法是什么? - What is the best way to manage roles in ActiveDirectory for ASP.NET Website? 在ASP.NET 2.0中管理设置(配置)的最佳方法是什么? - What is the best way to manage settings (configuration) in ASP.NET 2.0? 在ASP.NET MVC中管理用户的最佳方法是什么 - What is the best way to manage users in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM