开发者

How to configure cache regions in fluent nhibernate and syscache2

开发者 https://www.devze.com 2023-01-02 07:09 出处:网络
I\'ve been trying to implement cache regions with fluent nhibernate and I\'ve done the following so far:

I've been trying to implement cache regions with fluent nhibernate and I've done the following so far:

1) Setup caching in Fluently.Configure():

private static ISessionFactory CreateSessionFactory()
{
    string csStringName = Environment.MachineName;

    var nhibConfigProps = new Dictionary<string, string>();
    nhibConfigProps.Add("current_session_context_class","web");

    var cfg = Fluently.开发者_如何学CConfigure()
        .Database(MsSqlConfiguration.MsSql2008
                      .ConnectionString(c => c.FromConnectionStringWithKey(csStringName))
                      .ShowSql()
                      .Cache(cache=>cache.ProviderClass<NHibernate.Caches.SysCache2.SysCacheProvider>().UseQueryCache()))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>())
        .ExposeConfiguration(config => config.AddProperties(nhibConfigProps))
        .ExposeConfiguration(config=> config.EventListeners.DeleteEventListeners = new IDeleteEventListener[] {new SoftDeleteListener()})
        .ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
        .BuildSessionFactory();

    return cfg;
}

2) Changed my ClassMap to enable cache, and set the region of choice:

 public UserMap()
 {
     Cache.ReadWrite().Region("User");
     ...
 }

Hopefully I've done the above correctly, but I can't really figure out where to configure the priority and cache duration for each region. Do you know how to do that? And if you happen to find flaws in the above code I'd really appreciate the feedback.


You will need to add the priority and expiration time for this region in the syscache configuration in web/app.config. Take a look at this excellent post for a great explanation of using second level cache. The examples use vanilla NHibernate but you should get the idea - the bit about configuring syscache is at the end of the post.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号