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

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

问题描述

有没有人举个例子,如何在 fluent 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 Mappings,并使用 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 
    }
}

我的用户类图如下所示:

My user class map looks like this:

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 以使用二级缓存.所以在我流畅的配置中,我有:

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 探查器,当我获得用户的国家/地区列表时,它们会加载一次,并且在每次其他请求后都会获得缓存命中.好消息是,当显示用户国家名称时,它从缓存中加载,并且不会向数据库发出请求.我从 加布里埃尔·申克.希望有帮助吗?如果您找到了更好/正确的方法,请告诉我?谢谢!

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天全站免登陆