Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 缓存助手类是单例模式的候选类吗?_C#_Asp.net_Design Patterns_Appfabric - Fatal编程技术网

C# 缓存助手类是单例模式的候选类吗?

C# 缓存助手类是单例模式的候选类吗?,c#,asp.net,design-patterns,appfabric,C#,Asp.net,Design Patterns,Appfabric,我最近下载了一些用于AppFabric缓存的示例。我注意到在示例中,他们使用了一个带有静态方法的类,而不是单例 出于以下原因,我考虑将其改为单亲: 惰性负载 只有一个缓存实例。。。我想不出为什么需要不止一个实例。 我是不是离目标太远了,还是钱花得对 下面是他们包括的一个课程: public class CacheUtil { private static DataCacheFactory _factory = null; private static DataCache _cache =

我最近下载了一些用于AppFabric缓存的示例。我注意到在示例中,他们使用了一个带有静态方法的类,而不是单例

出于以下原因,我考虑将其改为单亲:

惰性负载 只有一个缓存实例。。。我想不出为什么需要不止一个实例。 我是不是离目标太远了,还是钱花得对

下面是他们包括的一个课程:

public class CacheUtil
{
  private static DataCacheFactory _factory = null;
  private static DataCache _cache = null;
  public static DataCache GetCache()
  {
      if (_cache != null)
          return _cache;

      //-------------------------
      // Configure Cache Client 
      //-------------------------

      //Define Array for 1 Cache Host
      List<DataCacheServerEndpoint> servers = 
          new List<DataCacheServerEndpoint>(1);

      //Specify Cache Host Details 
      //  Parameter 1 = host name
      //  Parameter 2 = cache port number
      servers.Add(new DataCacheServerEndpoint("localhost", 22233));

      //Create cache configuration
      DataCacheFactoryConfiguration configuration = 
          new DataCacheFactoryConfiguration();

      //Set the cache host(s)
      configuration.Servers = servers;

      //Set default properties for local cache (local cache disabled)
      configuration.LocalCacheProperties = 
          new DataCacheLocalCacheProperties();

      //Disable tracing to avoid informational/verbose messages on the web page
      DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

      //Pass configuration settings to cacheFactory constructor
      _factory = new DataCacheFactory(configuration);

      //Get reference to named cache called "default"
      _cache = _factory.GetCache("default");

    return _cache;
  }

我会说是的,我在我们的web应用程序中针对我们自己的缓存接口使用了单例模式,我不明白为什么要将类(看起来是静态工厂)更改为单例。它还进行延迟加载,并且不会有多个实例

编辑


工厂方法甚至更好,因为它至少会返回一个接口,所以它可以在以后的版本中更改它的实现,而不会破坏客户端代码。

是的。它很容易实现:

    public class CacheUtil
    {
        private static DataCacheFactory _factory = null;
        private static DataCache _cache = null;

        // This is the single instance of this class
        private static readonly CacheUtil instance = new CacheUtil();

        private CacheUtil()
        {
            _cache = GetCache();
        }        

        /// <summary>
        /// Provides the single reference point to access this class
        /// </summary>
        public static CacheUtil Instance
        {
            get { return instance; }
        }

        private static DataCache GetCache()
        {
            if (_cache != null)
                return _cache;

            //-------------------------
            // Configure Cache Client 
            //-------------------------

            //Define Array for 1 Cache Host
            List<DataCacheServerEndpoint> servers =
                new List<DataCacheServerEndpoint>(1);

            //Specify Cache Host Details 
            //  Parameter 1 = host name
            //  Parameter 2 = cache port number
            servers.Add(new DataCacheServerEndpoint("localhost", 22233));

            //Create cache configuration
            DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration { 
Servers = servers, 

LocalCacheProperties = new DataCacheLocalCacheProperties() };

            //Disable tracing to avoid informational/verbose messages on the web page
            DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

            //Pass configuration settings to cacheFactory constructor
            _factory = new DataCacheFactory(configuration);

            //Get reference to named cache called "default"
            _cache = _factory.GetCache("default");

            return _cache;
        }
        /// <summary>
        /// Gets the cache
        /// </summary>
        public DataCache Cache { get; private set; }
    }

好名字:。我不明白你说的工厂方法是什么意思。工厂将生成哪些对象?您希望更改类,因为它不是线程安全的。我也喜欢工厂模式。所以我对它进行了重构,保留了完整的接口,但是使用了Lazy类型——见第六版,它基本上已经是一个单例了。它具有单例的所有特性,并且不会创建多个实例。单例的这种实现不是线程安全的。