NHibernate L2缓存:代码配置

3

如何在代码中配置二级缓存(而不是通过XML)

我目前的设置如下:

 public NHCachingSetup(Configuration cfg)
        {
            // use first level cache
            cfg.Cache(x =>
            {
                x.UseQueryCache = true;
                x.Provider<SysCacheProvider>();
            });

            // set 60 min expiration time
            cfg.SessionFactory().Caching
                .WithDefaultExpiration(60);
        }
2个回答

3
我使用NH 3.3的方法是这样的:
var configure = new Configuration();
...
configure.Cache(x => x.UseQueryCache = true)
...
configure.SessionFactory().Caching
  .Through<SysCacheProvider>().WithDefaultExpiration(1440);//secs!

在您的映射中进行编辑,您将需要以下内容:
Cache(x => x.Usage(CacheUsage.ReadOnly));

然后,你可以这样使用(这里是为我缓存查找表):-
Db.Query<SpamAssassin>().Cacheable().CacheMode(CacheMode.Normal).ToList();

但是没有L2缓存配置...我一开始就无法启用L2缓存... - user2165424
抱歉,我不理解你的评论。你可以从我提供的代码中启用它。 - Rippo
缺失的是 .SetProperty(Environment.UseSecondLevelCache, "true")。 - user2165424
1
你也可以按照我展示的方式来做,你的方式是旧的设置方法。 - Rippo

0

明白了:

cfg.SetProperty(Environment.BatchSize, "100")
                .SetProperty(Environment.UseQueryCache, "true")
                .SetProperty(Environment.UseSecondLevelCache, "true")
                .Cache(x => { x.Provider<SysCacheProvider>(); });

你使用的 nh 版本是哪个? - Rippo
@rippo nh 版本 3.3.3.. 是最新版本。 - user2165424

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接