Singleton pattern in persistent cache in memory

Using what I rated was the best of all worlds on Implementing the Singleton Template in C # , a terrific article, I successfully use the following class to store user data in memory (for very rarely modified data):

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

}

My usage would look something like this:

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

Whenever I update the database, RefreshLocalizations is called, so only part of my storage in memory is restored. I have one out of 10 production environment that seems to behave badly when RefreshLocalizations is called rather than refreshing at all, but it also seems choppy and very strange in general.

, , , , , singleton, RAM .

, :

  • , , , , , (?).
  • IIS , (, Web Garden?)
  • - , , .

?

.NET 3.5, , Reactive Extensions

Edit1: :

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

, Localizations :

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

}
+2

, .

_localization, (). , singleton, - .

( ) Localization. , , , .

, :

    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();

, , , .

, , - . 32- , .

, :

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.

+2

All Articles