nhibernate - Fluent NHibernate Syscache2 缓存过期

标签 nhibernate caching fluent-nhibernate syscache2

我一直致力于使用 fluent 和 syscache2 的 WCF 服务。关于我目前的困境,我几乎阅读了所有关于 SO 的文章;我运气不好。

我正在尝试为我的二级缓存设置过期时间。无论我设置什么值似乎都会被忽略,默认值 5 分钟用于使缓存过期。

流畅的配置:

注意:contextClass 只是一个描述符类,用于保存传递给配置的值。

var cfg = Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008                        
                    .ConnectionString(c => c.Is(connectionString))
                    .ShowSql()
                    )
                .Diagnostics(d => d.Enable())                                                             
                .Cache(c => c                                 
                            .UseQueryCache()          
                            .ProviderClass(typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName))                    
                .Mappings(m => m
                    .FluentMappings
                    .AddFromAssembly(assembly)) 
                .ExposeConfiguration(x =>
                {
                    x.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, contextClass.Id);
                    x.SetProperty(NHibernate.Cfg.Environment.PrepareSql, contextClass.PrepareSql); //set prepare_sql true/false
                    x.SetProperty(NHibernate.Cfg.Environment.CacheDefaultExpiration, contextClass.ExpireL2Cache); //set default expiration in seconds
                });

我还将 app.config 文件设置如下:

<configSections>
  <section name="syscache" type="NHibernate.Caches.SysCache2.SysCacheSection, NHibernate.Caches.SysCache2"/>
</configSections>

<syscache>
  <cache expiration="600" priority="5" />
</syscache>

有一个 app.config 的变体,它有一个使用区域的 syscache 部分,但它也不起作用。

有人对想法有什么建议吗?

谢谢

最佳答案

我一直用这个没有问题:

.ExposeConfiguration (cfg => {
    cfg.Properties.Add ("expiration", "900");
})

不确定 Properties.Add 的行为是否与您正在使用的 SetProperty 调用有任何不同。

似乎如果您使用的是较新版本的 NHibernate,您也可以依靠 NHibernate.Cfg 命名空间中的新扩展方法(这将取代您的整个 .Cache 调用)

.ExposeConfiguration (cfg => {
    cfg.SessionFactory().Caching.Through<SysCacheProvider>().WithDefaultExpiration(900);
})

在阅读时我发现了 this :

cache.default_expiration or expiration (Int32): since NH Contrib 2.1 cache.default_expiration is the new setting name that should be used instead of expiration to specify number of seconds after which the cache item must be invalidated. Default value is 300 seconds. The old name is still supported for backward compatibility.

因此属性名称可能不是您的问题(现在想知道我使用的“过期”键是否也可能特定于内存缓存提供程序,尽管它似乎适用于系统缓存)。

关于nhibernate - Fluent NHibernate Syscache2 缓存过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730671/

相关文章:

docker - 如何防止 docker build 重新下载复制的 Go vendor

caching - Varnish 缓存-缓存403响应

已经加载时的 Angular 缓存图像?

c# - NHibernate 无法删除子实体

.net - SQL Server 2008 层次结构数据类型的 NHibernate 映射

c# - Nhibernate 项目转化为类属性

database - 是否可以使用 NHibernate 创建数据库?

NHibernate:在读取过程中使用(var tx = session.BeginTransaction()){} 而不使用 Commit() 可以吗?

mysql - Asp.Net MVC UNitOfWork 和 MySQL 以及休眠连接

asp.net-mvc - 使用 ASP.NET、NHibernate/Fluent NHibernate 实现最轻松的 Multi-Tenancy