NHibernate二级缓存缓存实体没有缓存配置

编程入门 行业动态 更新时间:2024-10-04 23:24:06
本文介绍了NHibernate二级缓存缓存实体没有缓存配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我已经在会话工厂配置了二级缓存。但是对于POCO实体,我还没有启用缓存。

我使用Fluent NHibernate来配置SessionFactory和POCO实体。

以下是会话工厂的配置:

var cfg =流利的.Configure()数据库(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString) .Provider(NHibernate.Connection.DriverConnectionProvider,NHibernate) .Dialect< CustomMsSql2008Dialect>() .Driver< SqlAzureClientDriver> .ShowSql()) .Cache(x => { x.UseQueryCache(); x.UseSecondLevelCache()。ProviderClass< HashtableCacheProvider> ).UseMinimalPuts(); })

POCO实体配置如下: / p>

public CustomerConfiguration() {表(Sys_Customer); DynamicUpdate(); Id(x => x.Id).GeneratedBy.GuidComb()。UnsavedValue(Guid.Empty); Map(x => x.Code); Map(x => x.Name); Map(x => x.IsActive); Component(x => x.Contact).ColumnPrefix(Contact_); Component(x => x.Address).ColumnPrefix(Address_); Map(x => x.IsDeleted); Map(x => x.CreatedOn).Column(CreatedOn); Map(x => x.CreatedBy).Column(CreatedBy)。Length(100); Map(x => x.LastModifiedOn).Column(LastModifiedOn); Map(x => x.LastModifiedBy).Column(LastModifiedBy)。Length(100); 版本(x => x.RowVersion).UnsavedValue(0); $ b

因此,对于Sys_Customer实体,我清楚地没有配置任何缓存,但是当我运行以下代码:数据库只被命中一次: $ p $ 使用(ISession s = RepositorySession) { var f = s.Get< Customer>(new Guid(75EDC0C2-4E58-43FF-B0D8-8C52FBB2D502)); var d = f.Code; 使用(ISession s = RepositorySession) { var f = s.Get< Customer>(new Guid(75EDC0C2-4E58-43FF-B0D8 -8C52FBB2D502\" )); var d = f.Code; $ / code>

当我从SessionFactory中删除缓存配置时,数据库被击中两次。所以显然这个实体被缓存在二级缓存中。 有人可以告诉我如何避免将实体缓存到NHibernate的二级缓存?????? b $ b

解决方案

我有ClassConvention配置了二级缓存。它看起来如下: pre $ public class ClassConvention:IClassConvention { ///< summary> ///应用指定的实例。 ///< / summary> ///< param name =instance>实例< / param> public void Apply(IClassInstance instance) { instance.Table(instance.EntityType.Name); instance.LazyLoad(); instance.Cache.NonStrictReadWrite(); $ p

这导致实体被缓存到第二个级高速缓存。我从IClassConvention中删除了配置,现在它正在按预期工作

I have configured second level cache on the session factory. However for the POCO entity I have not enabled caching.

I am using Fluent NHibernate for configuring the SessionFactory and the POCO entities.

Here is the configuration of the session factory:

var cfg = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString) .Provider("NHibernate.Connection.DriverConnectionProvider, NHibernate") .Dialect<CustomMsSql2008Dialect>() .Driver<SqlAzureClientDriver>() .ShowSql()) .Cache(x => { x.UseQueryCache(); x.UseSecondLevelCache().ProviderClass<HashtableCacheProvider>().UseMinimalPuts(); })

The POCO entity configuration is as follows:

public CustomerConfiguration() { Table("Sys_Customer"); DynamicUpdate(); Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty); Map(x => x.Code); Map(x => x.Name); Map(x => x.IsActive); Component(x => x.Contact).ColumnPrefix("Contact_"); Component(x => x.Address).ColumnPrefix("Address_"); Map(x => x.IsDeleted); Map(x => x.CreatedOn).Column("CreatedOn"); Map(x => x.CreatedBy).Column("CreatedBy").Length(100); Map(x => x.LastModifiedOn).Column("LastModifiedOn"); Map(x => x.LastModifiedBy).Column("LastModifiedBy").Length(100); Version(x => x.RowVersion).UnsavedValue("0"); }

So clearly for the Sys_Customer entity i have not configured any Caching, yet when i run the following code: the database is hit only once:

using (ISession s = RepositorySession) { var f = s.Get<Customer>(new Guid("75EDC0C2-4E58-43FF-B0D8-8C52FBB2D502")); var d = f.Code; } using (ISession s = RepositorySession) { var f = s.Get<Customer>(new Guid("75EDC0C2-4E58-43FF-B0D8-8C52FBB2D502")); var d = f.Code; }

when I remove the Cache Configuration from the SessionFactory then the database is hit twice. so clearly the entity is getting cached in the second level cache.

Can someone tell me how to avoid caching the entity into the second level cache of NHibernate????

解决方案

I had ClassConvention that was configured with second level cache. it looked as follows:

public class ClassConvention : IClassConvention { /// <summary> /// Applies the specified instance. /// </summary> /// <param name="instance">The instance.</param> public void Apply(IClassInstance instance) { instance.Table(instance.EntityType.Name); instance.LazyLoad(); instance.Cache.NonStrictReadWrite(); } }

this was causing the entity to be cached into the second level cache. i removed the configuration from IClassConvention and now it is working as expected

更多推荐

NHibernate二级缓存缓存实体没有缓存配置

本文发布于:2023-11-26 18:57:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   实体   二级缓存   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!