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"/>
        ....
    </set>
</class>

 

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)
    .SetMaxResults(15)
    .SetCacheable(true)
    .SetCacheRegion("frontpages")
    .List();


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: 
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/caches.html
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/...
http://using.castleproject.org/display/AR/Enable+second+level+cache
to understand how Level2 cache working:

Hibernate: Truly Understanding the Second-Level and Query Caches

 

Hibernate HQL And Performance

http://ayende.com/Blog/category/510.aspx 
 

 
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:
[code]
peopleList.Sort(
delegate(Person p1, Person p2)
{
return p1.name.CompareTo(p2.name);
}
);

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

[/code]
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九拍
如果不用匿名方法,我们1.1里就的这么写

代码:
static int Compare(Person p1, Person p2)
{
return p1.name.CompareTo(p2.name);
}
然后
代码:
people.Sort(new Comparison<Preson>(Compare));
你的问题解决了,在C#3.0里,我们不再需要匿名方法了,直接lambda咯
代码:
people.Sort((p1, p2) => p1.name.CompareTo(p2.name));

Thanks to Shadal:
2.0可以这样写
static int Compare(Person p1, Person p2)
{
return p1.name.CompareTo(p2.name);
}

people.Sort(Compare);





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)                
        this.InternalScripts.Add(script);
    else
        match = script;
} 


 
Categories: C# | LINQ and C# 3.0