3

I've been working on a WCF service that uses fluent and syscache2. I've pretty much read every article on SO regarding my current dilemma; I've had no luck.

I am trying to set the expiration time for my second-level cache. Whatever value I set seems to be ignored and the default value of 5 minutes is used to expire the cache.

Fluent configuration:

Note: contextClass is just a descriptor class holding values passed to the configuration.

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
                });

I also have the app.config file set up as following:

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

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

There was a variant of the app.config which had a syscache section that used regions but that didn't work either.

Anyone have any suggestions on ideas?

Thanks

Samir Banjanovic
  • 400
  • 1
  • 4
  • 16

1 Answers1

4

I've always used this without problems:

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

Not sure if Properties.Add behaves any differently than the SetProperty call you're using though.

It seems like if you're using a newer version of NHibernate you can lean on the new extension methods in the NHibernate.Cfg namespace for this as well (this would replace your entire .Cache call in fluent)

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

Doing some reading I found 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.

So the property name is probably not your issue (wondering now if the "expiration" key that I used was maybe specific to the memcache provider as well, though it seemed to work with syscache).

AlexCuse
  • 18,008
  • 5
  • 42
  • 51
  • 1
    SetProperty is equivalent to writing Properties["keyName"]. When I look at the Environment class I was able to find the following: ` public const string CacheDefaultExpiration = "cache.default_expiration"; ` I tried doing the fluent configuration you mentioned. However, I am not able to access the session factory in the configuration as I am setting. The code is currently using NH 3.2.0.400 and fNH 1.3.0.717 – Samir Banjanovic Jul 31 '12 at 15:38
  • Hmm maybe that is newer, though I thought it would be available in 3.0+. Are you sure you're referencing the NHibernate.Cfg namespace? If not you won't have access to the extension methods off of the configuration. – AlexCuse Jul 31 '12 at 16:35
  • I think I got it working. I am not sure though...you're tip definitely helped...as it turns out the NHibernate.Cfg namespace was missing. Thanks a lot! – Samir Banjanovic Aug 03 '12 at 03:02