I made a sample project to test nhibernate performance with second level
cache.
The classes are User ->* Blog ->* Post (->* = one-to-many)
I used SQL Profiler to check how many times the DB was hit
What I discovered:
this code
Code:
ICriteria crit = session.CreateCriteria(typeof(User)).SetCacheable(true);
IList users = crit.List();
causes a single hit on the DB with a select
if i do the same thing again:
Code:
crit = session.CreateCriteria(typeof(User)).SetCacheable(true);
users = crit.List();
the DB doesn't get hit, but i gues it could be from the first-level cache (is it?)
However, doing this
Code:
crit = session.CreateCriteria(typeof(User));//no SetCacheable(true)
users = crit.List();
the DB gets hit and i guess that's ok :D
I also tried tu cache a query and the cache worked.
The strange thing is that in this code:
Code:
crit = session.CreateCriteria(typeof(User));//no SetCacheable(true)
users = crit.List();
User u = new User("CodeMonkey");
session.Save(u);
crit.List();
the DB gets hit on the last
Code:
crit.List()
I was expecting the SLC (second level cache) gets updated
from session Save or Update. Does it depend on the cache provider?
I used MemCached for all this and the performance was weaker
than using the HashtableCacheProvider.
After doing all these tests (and more) i have a question, maybe
someone will save me some time by answearing:
is it worth using the SLC?
The documentation doesn't offer much light on the caches.
I didn't find a good sample on the net...
Any advice on this?
(sorry for my English)