简体   繁体   English

如何在fluent nhibernate和syscache2中配置缓存区域

[英]How to configure cache regions in fluent nhibernate and syscache2

I've been trying to implement cache regions with fluent nhibernate and I've done the following so far: 我一直在尝试使用流畅的nhibernate实现缓存区域,到目前为止,我已经完成了以下工作:

1) Setup caching in Fluently.Configure(): 1)在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.Configure()
        .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: 2)更改了我的ClassMap以启用缓存,并设置了选择的区域:

 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. 您将需要在web / app.config的syscache配置中为该区域添加优先级和到期时间。 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. 这些示例使用普通的NHibernate,但您应该了解一下-有关配置syscache的内容在文章结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM