[ErrataKorrige]: La Cache e oggetti singleton

Piccola modifica al codice che avevo postato in "La Cache e oggetti singleton" perchè non si sa mai che la CacheItemRemovedCallback scatti prima che la funzione sia riuscita a tornare l'istanza statica e quindi vi ritroviate con un bel NullReferenceException.
class C
{
   static C c_currentInstance;
   
   public static C Current
   {
      C instance =  c_currentInstance;
      if(instance == null)
      {
         instance = new C();
         c_currentInstance = instance;
         HttpContext.Current.Cache.Add("c_currentInstance", instance, [cut], 
           new CacheItemRemovedCallback(OnCacheItemRemovedCallback));
      }
      return instance;
   }
   static void OnCacheItemRemovedCallback([cut])
   {
       c_currentInstance = null;
   }
}

posted @ mercoledì 26 aprile 2006 16:52

Print

Comments on this entry:

# re: [ErrataKorrige]: La Cache e oggetti singleton

Left by M.rkino at 26/04/2006 19:29
Gravatar
ciao Marco, ti posto la risposta che avevo dato a una stessa domanda a Novembre. -> La scelta della Cache non è infatti adottata in tutti i progetti ma "in alcuni nostri progetti". Solitamente la scelta di usare la cache è legata a progetti web con eventuali dipendenze e/o expiration e/o sliding time.
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011