我一直在尝试使用流利的NHIBERNATE实施缓存区域,到目前为止,我已经完成了以下操作:

1)在流利的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)更改了我的分类图以启用缓存,并设置了选择的区域:

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

希望我已经正确完成了上述操作,但是我无法真正弄清楚在哪里配置每个区域的优先级和缓存持续时间。你知道怎么做吗?而且,如果您碰巧在上述代码中发现缺陷,我将非常感谢反馈。

有帮助吗?

解决方案

您需要在Web/app.config中的SYSCACHE配置中为该区域添加此区域的优先级和到期时间。看一眼 这个出色的帖子 对于使用第二级缓存的很好解释。这些示例使用香草nhibrenate,但您应该得到这个想法 - 关于配置Syscache的一些内容是在帖子的末尾。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top