August 29, 2008
@ 10:41 PM
I have enabled the level2 cache for one of my old project "Asset  Management".

here are some important points to use the L2 cache:

1.  add this line to web.config nhibernate section

  <add key="hibernate.cache.provider_class"
       value="NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache" />
  <add key="relativeExpiration" value="300" />

2. NHibernate.Caches.SysCache.dll have different version. it can be download from nhibernate contribution.

I didn't have one to match my nhibernate version, if you can't found the right version, you  download the source code and rebuilt it with the current nhibernate.

3. example of set entity definition file *.hbm

<class name="eg.Cat" .... >
    <cache usage="read-write"/>
    <set name="Kittens" ... >
        <cache usage="read-write"/>


There are readonly|read-write|nonstrict-read-write strategies.

readonly for entities never be modified.

nonstrict-read-write for entities  be update occasionally(don't need worry about two trsaction update same entity)


4. the query cache is not recommended, because it stateless, but good for query the "readonly"  database tables

<add key="hibernate.cache.use_query_cache" value="true" />

IList blogs = sess.CreateQuery("from Blog blog where blog.Blogger = :blogger")
    .SetEntity("blogger", blogger)

call IQuery.SetForceCacheRefresh(true) to reload the query, it is particularly if the table is updated outside of the nhibernate.
for the current Castle ActiveRecord, the nhibernate is 1.2, here is the doc:
to understand how Level2 cache working:

Hibernate: Truly Understanding the Second-Level and Query Caches


Hibernate HQL And Performance 

Categories: nHibernate

Sometime I need resort the ArrayList or List<T> in my code. I am too lazy to write a piece of code to implement IComparer. When I search through google, I found there is a very elegant way to do the sorting:
delegate(Person p1, Person p2)

peopleList.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age,; });

I have used code like this many times, but actually I didn't really understand why code can be wrote like this.
Those are some different way to sorting in different version of .net frameworks,
and those articles helping me to understand the sorting code better.

Thanks to happy九拍

static int Compare(Person p1, Person p2)
people.Sort(new Comparison<Preson>(Compare));
people.Sort((p1, p2) =>;

Thanks to Shadal:
static int Compare(Person p1, Person p2)


An userful article:

Variable Scoping in Anonymous Delegates in C#

public void AddScript(ScriptItem script)
    ScriptItem match = null;
    // *** Grab just the path
    if (!string.IsNullOrEmpty(script.Src))
        script.FileId = Path.GetFileName(script.Src).ToLower();
        match = this.InternalScripts.Find(
            delegate(ScriptItem item)
               ScriptRenderModes mode = this.RenderMode;  // demonstrate this pointer
               return (item.FileId == script.FileId);
    if (match == null)                
        match = script;

Categories: C# | LINQ and C# 3.0