English 中文(简体)
A. 单州内长期笼罩的格局
原标题:Singleton pattern on persistent in-memory cache

采用我所判断的最好办法,在http://www.yoda.arachsys.com/csharp/singleton.html”上“rel=“nofollow” 在C#执行单一吨位模式令人惊讶的文章中,我成功地利用了以下类别来保存用户界定的数据(关于很少修改的数据):

public class Params
{
  static readonly Params Instance = new Params();

  Params()
  {
  }

  public static Params InMemory
  {
    get
    {
      return Instance;
    }
  }

  private IEnumerable<Localization> _localizations;

  public IEnumerable<Localization> Localizations
  {
    get
    {
      return _localizations ?? (_localizations = new Repository<Localization>().Get());
    }
  }

  public int ChunkSize
  {
    get
    {
      // Loc uses the Localizations impl
      LC.Loc("params.chunksize").To<int>();
    }
  }

  public void RebuildLocalizations()
  {
    _localizations = null;
  }

  // other similar values coming from the DB and staying in-memory,
  // and their refresh methods

}

我的用意是这样:

var allLocs = Params.InMemory.Localizations; //etc

电话 我更新了该数据库,重新启用了Refresh Localizations,因此,仅重置了我内部储存的一部分。 在大约10个的生产环境中,我有一个single生产环境,当Refresh Localizations打电话时,这种环境就会发生错觉,而根本不恢复,但这似乎也是间断和非常奇怪的。

我目前对单一州持怀疑态度,我认为这项工作非常艰巨,所有单位测试都证明,单一州机制、复习机制和援助团的业绩都是预期的。

尽管如此,我还是看着这些可能性:

  1. This customer is lying when he says their environment is not using loading balance, which is a setting I am not expecting the in-memory stuff to work properly (right?)
  2. There is some non-standard pool configuration in their IIS which I am testing against (maybe in a Web Garden setting?)
  3. The singleton is failing somehow, but not sure how.

任何建议?

<>strong>NET 3.5, 如此之不相平行,现在还不准备使用追溯式延长。

Edit1:根据建议,新老会看像:

public IEnumerable<Localization> Localizations
{
  get
  {
    lock(_localizations) {
      return _localizations ?? (_localizations = new Repository<Localization>().Get());
    }
  }
}
最佳回答

为了扩大我的评论,你可以如何使<编码> 当地化财产安全:

public class Params
{
  private object _lock = new object();

  private IEnumerable<Localization> _localizations;    
  public IEnumerable<Localization> Localizations
  {
    get
    {
      lock (_lock) {
         if ( _localizations == null ) {
            _localizations = new Repository<Localization>().Get();
         }

         return _localizations;
      }
    }
  }

  public void RebuildLocalizations()
  {
     lock(_lock) {
        _localizations = null;
     }
  }

  // other similar values coming from the DB and staying in-memory,
  // and their refresh methods

}
问题回答

There is no point in creating a thread safe singleton, if your properties are not going to be thread safe.

您要么围绕<条码>——“地方化”/条码的指定,要么在您的单一吨构造中即时(p. 参考)。 适用于单一州瞬时状态的任何建议均适用于这一经证实的财产。

The same thing further applies to all properties (and their properties) of Localization. If this is a Singleton, it means that any thread can access it any time, and simply locking the getter will again do nothing.

例如,审议本案:

    Thread 1                              Thread 2

    // both threads access the singleton, but you are "safe" because you locked
1.  var loc1 = Params.Localizations;      var loc2 = Params.Localizations;

    // do stuff                           // thread 2 calls the same property...
2.  var value = loc1.ChunkSize;           var chunk = LC.Loc("params.chunksize");

    // invalidate                         // ...there is a slight pause here...
3.  loc1.RebuildLocalizations();

                                          // ...and gets the wrong value
4.                                        var value = chunk.To();

如果你只读这些价值观,那么它可能不是问题,但你可以看看你如何容易地对待这种做法。

如果听起来,你就永远不知道,在两项指示之间,是否有一条不同的线。 www.un.org/Depts/DGACM/index_french.htm

This means that, in this line here:

return LC.Loc("params.chunksize").To<int>();

就阅读而言,相当于:

var loc = LC.Loc("params.chunksize");
Thread.Sleep(1); // anything can happen here :-(
return loc.To<int>();

任何校对可在<代码>Loc和之间跳跃。 To





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...