2017-06-04 87 views
0

我使用FluentNhibernate版本2.0.3.0,我試圖配置它的緩存與queries用,我使用HQL。如果我做了所有配置,我想要一個意見,並且我想知道如何才能看到緩存是否被激活?FluentNHibernate的緩存配置?

連接

FluentConfiguration _config = Fluently.Configure() 
    .Database(
     MySQLConfiguration.Standard.ConnectionString(
      x => x.Server(HOST) 
        .Username(USER) 
        .Password(PASSWORD) 
        .Database(DB))) 
    .Cache(c => c.ProviderClass(typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName) 
       .UseQueryCache()) 
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UsuarioMap>()) 
    .ExposeConfiguration(cfg => new SchemaUpdate(cfg) 
    .Execute(false, true)); 

映射

public class UsuarioMap : ClassMap<Usuario> { 
    public UsuarioMap() { 
     Table("USUARIOS"); 
     Cache.ReadWrite(); 
     Id(u => u.id).GeneratedBy.Native(); 
     Map(u => u.nome).Not.Nullable().Length(50); 
     Map(u => u.email).Unique().Not.Nullable().Length(255); 
     Map(u => u.senha).Not.Nullable(); 
     Map(u => u.status).CustomType<int>(); 
     Map(u => u.dtCadastro).CustomType<DateTime>(); 
     Map(u => u.dtLastLogin).CustomType<DateTime>(); 
     Map(u => u.tipo).CustomType<int>(); 
     Map(u => u.imagem); 
     Map(u => u.loginBy).CustomType<int>(); 
     Map(u => u.idFacebook); 
    } 
} 

HQL查詢

public class UsuarioDAO : GenericDAO<Usuario>{ 

    /** retorna todos os objetos */ 
    public IList<Usuario> findAll(){ 
     ISession _session = getSession(); 
     IList<Usuario> list = _session.CreateQuery("FROM Usuario u ORDER BY u.nome") 
      .SetCacheable(true).SetCacheMode(CacheMode.Normal) 
      .List<Usuario>(); 
     return list; 
    } 

回答

1

UseSecondLevelCache甲調用很可能會丟失(xml配置參數cache.use_second_level_cache)。

HashtableCacheProvidernot intended for production use,僅用於測試。選擇another one

交易似乎並沒有使用。首先在事務外完成數據更改,二級緩存將被禁用。更好地處理所有會話使用。

應配置A cache.default_expiration。這是一個整數,以秒爲單位。

您可以使用會話統計數據,以檢查是否使用緩存。在會話工廠啓用它們。

sessionFactory.Statistics.IsStatisticsEnabled = true; 

// Do your stuff then consult the statistics on the session factory. 

避免讓他們在生產中啓用,這會帶來一些開銷。

其他注意事項:

如果你希望緩存一些映射集合,你將不得不爲他們啓用緩存在他們的映射。

緩存可與延遲加載更好。預加載可能導致緩存實體從數據庫重新加載,而不是從緩存加載。