缓存辅助类是单例模式的候选者吗?

发布于 2024-11-02 10:21:52 字数 1638 浏览 1 评论 0原文

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

我正在考虑将其更改为单例,原因如下:

  1. 延迟加载
  2. 只有一个缓存实例...我想不出为什么需要多个实例的原因。

我是偏离目标还是正确赚钱?

下面是他们包含的一个类:

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

I recently downloaded some samples to use with AppFabric caching. I noticed in the sample they used a class with static methods instead of a singleton.

I was thinking of changing it to a singleton for the following reasons:

  1. lazy load
  2. Only one instance of cache... I can't think of a reason why more than one instance is needed.

Am I way off target or right on the money?

Below is a class they included:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木槿暧夏七纪年 2024-11-09 10:21:53

我想说是的,我在我们的网络应用程序中使用单例模式作为缓存(针对我们自己的缓存接口)

I would say yes, I use a singleton pattern for the Cache in our web app (against our own cache Interface)

回眸一笑 2024-11-09 10:21:53

我不明白为什么你想将这个似乎是静态工厂的类更改为单例。它还执行延迟加载,并且不会有多个实例。

编辑

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

I don't see why you want to change the class, which seems to be a static factory, into a Singleton. It also does lazy loading, and there also won't be more than one instance.

EDIT

The factory approach is even better in that it returns an interface (at least I would guess so), so it could change it's implementation in later releases without breaking client code.

空心空情空意 2024-11-09 10:21:52

是的。它很容易实现:

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

Yes. It is easy to implement:

    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; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文