strange issue with cache application block (ENT LIB 5)
-
Hi all, I am relatively new to Caching application block, so my problem can be very trivial. But I don't get it. So there is this complex application (in production enviroment) that I have rewritten to use cache. my framework is not so easy to describe, however: every service class (in the logic tier) has a father class that define the cache (static, so application wide)
SmallObjectCache = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>("SmallObjectCache");
I have written some classes to manage my cache: I use for single objects and for list of object (Read operation and GetAll operation)
protected virtual void CacheAddData(DomainObject domainObject)
{
if (domainObject != null)
{
CacheDictionary[this.GetType().FullName].Add(this.GetType().FullName + "_" + domainObject.GetId(), domainObject);List<DomainObject> cachedObj = CacheGetAllData(); if (cachedObj != null && cachedObj.Count > 0) { int idx = -1; try { idx = cachedObj.FindIndex(a => a.GetId() == domainObject.GetId()); cachedObj.RemoveAll(a => a.GetId() == domainObject.GetId()); } catch (NullReferenceException) { BusinessFacade.ApplicationLogger.Write("INFO: Cache: Linq exception", Category.General, Priority.Normal, 1, System.Diagnostics.TraceEventType.Information); } if (idx >= 0) cachedObj.Insert(idx, domainObject); else cachedObj.Add(domainObject); CacheAddAllData(cachedObj); } }
}
So what the above do is to add an item in cache and if exist a list of that objects retrive it and add/insert in the right position the item. Now the bug: Everything is running fine. 100+ people use the system without a glitch. BUT sometime, near middle of the day, one or two people report a nullreference exception, especially in this code:
List<Utente> decoratori = GetAll().Cast<Utente>().ToList().Where<Utente>(u => u.Ruolo.IsDecoratore()).ToList();
the where iterator crash stating that object is null (I do checks before the only null objects can be the "Ruolo") Now this is the issue: the same user with everyting unchanged became without an attribute (non deterministic). Recycling the IIS memory fix the issue (so is not a reproducible issue).
-
Hi all, I am relatively new to Caching application block, so my problem can be very trivial. But I don't get it. So there is this complex application (in production enviroment) that I have rewritten to use cache. my framework is not so easy to describe, however: every service class (in the logic tier) has a father class that define the cache (static, so application wide)
SmallObjectCache = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>("SmallObjectCache");
I have written some classes to manage my cache: I use for single objects and for list of object (Read operation and GetAll operation)
protected virtual void CacheAddData(DomainObject domainObject)
{
if (domainObject != null)
{
CacheDictionary[this.GetType().FullName].Add(this.GetType().FullName + "_" + domainObject.GetId(), domainObject);List<DomainObject> cachedObj = CacheGetAllData(); if (cachedObj != null && cachedObj.Count > 0) { int idx = -1; try { idx = cachedObj.FindIndex(a => a.GetId() == domainObject.GetId()); cachedObj.RemoveAll(a => a.GetId() == domainObject.GetId()); } catch (NullReferenceException) { BusinessFacade.ApplicationLogger.Write("INFO: Cache: Linq exception", Category.General, Priority.Normal, 1, System.Diagnostics.TraceEventType.Information); } if (idx >= 0) cachedObj.Insert(idx, domainObject); else cachedObj.Add(domainObject); CacheAddAllData(cachedObj); } }
}
So what the above do is to add an item in cache and if exist a list of that objects retrive it and add/insert in the right position the item. Now the bug: Everything is running fine. 100+ people use the system without a glitch. BUT sometime, near middle of the day, one or two people report a nullreference exception, especially in this code:
List<Utente> decoratori = GetAll().Cast<Utente>().ToList().Where<Utente>(u => u.Ruolo.IsDecoratore()).ToList();
the where iterator crash stating that object is null (I do checks before the only null objects can be the "Ruolo") Now this is the issue: the same user with everyting unchanged became without an attribute (non deterministic). Recycling the IIS memory fix the issue (so is not a reproducible issue).
The thing about a cache is that you can't guarantee that it still holds data. A typical pattern with a cache is to attempt to retrieve an item from the cache, and if nothing is returned, the cache is refreshed with the data and then the item is returned. It sounds as though this is your issue. Basically, something you are trying to retrieve is no longer present in the cache, so you recycle IIS which causes you to repopulate the cache because you go through your normal start up processes. The only thing you can do to fix this is to add the functionality to check and reload the items.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Hi all, I am relatively new to Caching application block, so my problem can be very trivial. But I don't get it. So there is this complex application (in production enviroment) that I have rewritten to use cache. my framework is not so easy to describe, however: every service class (in the logic tier) has a father class that define the cache (static, so application wide)
SmallObjectCache = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>("SmallObjectCache");
I have written some classes to manage my cache: I use for single objects and for list of object (Read operation and GetAll operation)
protected virtual void CacheAddData(DomainObject domainObject)
{
if (domainObject != null)
{
CacheDictionary[this.GetType().FullName].Add(this.GetType().FullName + "_" + domainObject.GetId(), domainObject);List<DomainObject> cachedObj = CacheGetAllData(); if (cachedObj != null && cachedObj.Count > 0) { int idx = -1; try { idx = cachedObj.FindIndex(a => a.GetId() == domainObject.GetId()); cachedObj.RemoveAll(a => a.GetId() == domainObject.GetId()); } catch (NullReferenceException) { BusinessFacade.ApplicationLogger.Write("INFO: Cache: Linq exception", Category.General, Priority.Normal, 1, System.Diagnostics.TraceEventType.Information); } if (idx >= 0) cachedObj.Insert(idx, domainObject); else cachedObj.Add(domainObject); CacheAddAllData(cachedObj); } }
}
So what the above do is to add an item in cache and if exist a list of that objects retrive it and add/insert in the right position the item. Now the bug: Everything is running fine. 100+ people use the system without a glitch. BUT sometime, near middle of the day, one or two people report a nullreference exception, especially in this code:
List<Utente> decoratori = GetAll().Cast<Utente>().ToList().Where<Utente>(u => u.Ruolo.IsDecoratore()).ToList();
the where iterator crash stating that object is null (I do checks before the only null objects can be the "Ruolo") Now this is the issue: the same user with everyting unchanged became without an attribute (non deterministic). Recycling the IIS memory fix the issue (so is not a reproducible issue).
I had a problem like that but with xml reading and writing. As more bots crawled the site, at a very fast pace, my program would crash, because the file was still open. I had to really fine tune the program about 10 times, with more verification, and to open, read and close as fast as possible, then work in memory only, when done open write and close, then dump the memory and cleanup. I know it's not the same problem, but in principal, the code had to be as efficient as possible.