To cache or not to cache

Posts   
 
    
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 29-Jun-2009 23:35:15   

Hi, i have a general type question about .net cache.

is it a good practice to cache 1000+ entities on a single server? i have a Students table with 1000+ rows. GetStudent method looks into the active cache and returns the entity if it exists there. And if it doesnt exist in the active cache, it is fetched from the database and put into the active cache object.

is caching 1000+ entities a good practice over fetching the entity from the database every time is it requested? Would this overload the server and reduce the application performance or minimize the database trips and improve the performance?

thanks in advance -shane

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 30-Jun-2009 09:21:23   

First measure performance. If the DB activity is going to be huge for these entities (e.g. you fetch them a lot) and caching them gives more performance, it's better to cache. If not, don't. wink

The thing with caching is that it only works for single entity fetches and also only if you read the entities much more often than they're written.

Frans Bouma | Lead developer LLBLGen Pro
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 30-Jun-2009 17:55:02   

Otis wrote:

The thing with caching is that it only works for single entity fetches and also only if you read the entities much more often than they're written.

why would it only work for single entity fetches? i cant cache an entity collection? entities are read a lot more than they are written but its not that a students data is looked up 100+ times a day either.

any suggested tools or ways to measure and compare the performance gain/loss for both scenerios?

thanks -shane

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 30-Jun-2009 18:10:46   

e106199 wrote:

Otis wrote:

The thing with caching is that it only works for single entity fetches and also only if you read the entities much more often than they're written.

why would it only work for single entity fetches? i cant cache an entity collection?

In short: of course you can, but a collection retrieval is based on a filter, and you then have to filter the cache AND the db.

entities are read a lot more than they are written but its not that a students data is looked up 100+ times a day either.

Then I wouldn't bother and read it from the db.

any suggested tools or ways to measure and compare the performance gain/loss for both scenerios?

Profile the app, which can be a bit complicated in this case. The thing is that caching on a single entity basis only helps if the overhead of dealing with a cache (so checking if an entity is updated, if so, update the cache etc.) is lower than reading it from the db (which is very fast). Also, if you fetch collections of entities which are cached, you have a problem as you then have to filter cache + db contents, which is MORE overhead. See: http://weblogs.asp.net/fbouma/archive/2006/08/31/Why-a-cache-in-an-O_2F00_R-mapper-doesn_2700_t-make-it-fetch-data-faster_2E00_.aspx

Frans Bouma | Lead developer LLBLGen Pro
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 01-Jul-2009 01:59:51   

i think i understand.

one final questionflushed in my master page, at the top, i have 2 dropdown lists. one shows the academic year list and the second shows teh academic term list for the selected academic year. they are loaded in every page request since they are located in masterpage.

would it be a good idea to cache their data as collections? or maybe keep it in session and not deal with caching at all?

thanks -shane

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 01-Jul-2009 09:03:15   

e106199 wrote:

i think i understand.

one final questionflushed in my master page, at the top, i have 2 dropdown lists. one shows the academic year list and the second shows teh academic term list for the selected academic year. they are loaded in every page request since they are located in masterpage.

would it be a good idea to cache their data as collections? or maybe keep it in session and not deal with caching at all?

thanks -shane

That sounds like data which doesn't change once it's been created, so I think indeed that caching that would be a good idea. You could pre-create the controls however and cache that using asp.net cache, so it's more efficient

Frans Bouma | Lead developer LLBLGen Pro