在Fluent Nhibernate中设置实体和关系的缓存吗? [英] Set up caching on entities and relationships in Fluent Nhibernate?

查看:85
本文介绍了在Fluent Nhibernate中设置实体和关系的缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人举过一个例子,说明如何设置以及要在流畅的nhibernate中缓存哪些实体.都使用流利的映射和自动映射?

Does anyone have an example how to set up and what entities to cache in fluent nhibernate. Both using fluent mapping and auto mapping?

一对一和多对多的实体关系是否相同?

And the same for entity relationships, both one-to-many and many-to-many?

推荐答案

我一直在类似的情况下工作,我只想缓存特定的元素,并希望这些元素在启动时被加载一次并保存在缓存中,直到应用程序关闭.这是一个只读缓存,用于填充国家列表,以便用户可以从列表中选择他们的国家.

I have been working a a similar situation, where I just want to cache specific elements, and want these elements to be loaded once on start up, and kept in cache, until the application is shut down. This is a read only cache, and is used to populate a list of countries, so that a user can select their country from the list.

我使用了fluentNhibernate映射,并使用Cache.readonly()定义了Country作为我的班级

I used fluentNhibernate Mappings, and defined Country my class with Cache.readonly()

public class CountryMap : ClassMap<Country> {
    public CountryMap() { 
         Schema("Dropdowns");
         Cache.ReadOnly();
         // Class mappings underneath 
    }
}

我的用户类映射如下:

public class UserMap : ClassMap<User> {
    Id(x => x.Id).Column("UserId");
    Map(x => x.FirstName);
    Map(x => x.LastName);
    References(x => x.Country)
      .Column("CountryId");
}

我手动将Fluent Nhibernate配置为使用二级缓存.所以在我流利的Confuluation中,我有:

I manually configure Fluent Nhibernate to use Second level cache. So in my fluent Confuguration I have:

var sessionFactory = Fluently.Configure()
    .Database (...) // set up db here
    .Mappings(...)  //set up mapping here
    .ExposeConfiguration(c => {
        // People advice not to use NHibernate.Cache.HashtableCacheProvider for production
        c.SetProperty("cache.provider_class", "NHibernate.Cache.HashtableCacheProvider");
        c.SetProperty("cache.use_second_level_cache", "true");
        c.SetProperty("cache.use_query_cache", "true");
    })
    .BuildSessionFactory();

我已经签入SQL事件探查器,并且当我获得用户的国家/地区列表时,将一次加载国家/地区,并且在其他所有请求之后都得到缓存命中.令人高兴的是,当显示用户国家名称时,它会从缓存中加载,并且不会向数据库发出请求.我从 Gabriel Schenker .希望有帮助吗?如果您找到更好/合适的方法,请告诉我?谢谢!

I have checked in SQL profiler, and when I get a list of countrys for a user, the are loaded once, and I get cache hits after every other request. The nice thing is that when displaying the users country name, it loads from the cache, and does not make a request to the database. I got some tips from this posting by Gabriel Schenker. Hope that helps? If you found a better/proper way, please let me know? Thanks!

这篇关于在Fluent Nhibernate中设置实体和关系的缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆