Hi,
Sorry to bring up the subject of caching again as there seems to be many threads and even an article written by Otis on the matter. But I have a problem with the way the uniquing cache has been implemented and hope someone may be able to suggest something I can do.
To set the scene here is an example that highlights the problem that is causing my application to run slowly:
- I have an entity Person that has a property PersonType.
- _PersonType _is a object with the properties _Description _and Grade.
- _PersonType _is linked to _Person _via a foreign key in the database.
- There are only 3 rows in my _PersonType _table
In my code I want to do something like:
foreach (Person aPerson in personCollection)
{
string text = aPerson.PersonType.Description;
}
The problem occurs when _personCollection _starts getting larger, say 200 records, as each time I ask for the _PersonType _property it will hit the database generating 200+ SQL hits.
If there is a way of implementing a short lived cache (NHibernate refers to it as a level 1 cache) it would reduce my SQL hits from 200 to 3 (there are only three different types of Person) which is clearly a big saving.
At first glance it seemed a context would sort me out as this deals with uniquing but unfortunately although this ensures the you don’t re-instantiate the same object over and over again, it still hits the database to check the data is fresh.
This seems like an obvious requirement to me and one that has been overlooked in previous discussions. Am I missing something? Is there something I can do to stop my database being hammered?
Thanks