views:

252

answers:

1

Hi,

I've been trying to implement cache regions with fluent nhibernate and I've done the following so far:

  1. Setup caching in Fluently.Configure():

 

    private static ISessionFactory CreateSessionFactory() {
        string csStringName = Environment.MachineName;

        var nhibConfigProps = new Dictionary<string, string>();
        nhibConfigProps.Add("current_session_context_class","web");

        var cfg = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
                          .ConnectionString(c => c.FromConnectionStringWithKey(csStringName))
                          .ShowSql()
                          .Cache(cache=>cache.ProviderClass<NHibernate.Caches.SysCache2.SysCacheProvider>().UseQueryCache()))
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>())
            .ExposeConfiguration(config => config.AddProperties(nhibConfigProps))
            .ExposeConfiguration(config=> config.EventListeners.DeleteEventListeners = new IDeleteEventListener[] {new SoftDeleteListener()})
            .ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
            .BuildSessionFactory();

        return cfg;
        }
  1. Changed my ClassMap to enable cache, and set the region of choice:

 

     public UserMap()
     {
         Cache.ReadWrite().Region("User");
         ...
     }

Hopefully I've done the above correctly, but I can't really figure out where to configure the priority and cache duration for each region. Do you know how to do that? And if you happen to find flaws in the above code I'd really appreciate the feedback.

TIA//Marcus

+1  A: 

You will need to add the priority and expiration time for this region in the syscache configuration in web/app.config. Take a look at this excellent post for a great explanation of using second level cache. The examples use vanilla NHibernate but you should get the idea - the bit about configuring syscache is at the end of the post.

s1mm0t
OK, according to the post I cant configure caches through fluent nhibernate, so I will need to fix at least the session factory vanilla style. Thanks.
Marcus Oldin