我一直在尝试使用流畅的 nhibernate 实现缓存区域,到目前为止我已经完成了以下操作:
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)更改了我的 ClassMap 以启用缓存,并设置选择的区域:
public UserMap()
{
Cache.ReadWrite().Region("User");
...
}
希望我已经正确完成了上述操作,但我无法真正弄清楚在哪里配置每个区域的优先级和缓存持续时间。你知道怎么做吗?如果您碰巧发现上述代码中的缺陷,我将非常感谢您的反馈。
您需要在 web/app.config 的 syscache 配置中添加该区域的优先级和过期时间。请查看这篇优秀的文章,了解如何使用二级缓存。这些示例使用普通 NHibernate,但您应该明白 - 关于配置 syscache 的部分位于帖子末尾。