protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); var connectionMultiplexer = StackExchange.Redis.ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true"); //connectionMultiplexer.GetServer("localhost", 6379).FlushAllDatabases(); var cc = connectionMultiplexer.GetServer("localhost", 6379); RedisCacheProvider.SetConnectionMultiplexer(connectionMultiplexer); //RedisCacheProvider.SetOptions(new RedisCacheProviderOptions() //{ // Serializer = new NetDataContractCacheSerializer(), // CacheConfigurations = new[] // { // new RedisCacheConfiguration("NHibernate.Cache.StandardQueryCache") // { // Expiration = TimeSpan.FromSeconds(9) // } // } //}); var options = new RedisCacheProviderOptions(); options.Serializer = new NhJsonCacheSerializer(); options.KeyPrefix = "Sop:"; options.CacheConfigurations = new[] { new RedisCacheConfiguration("StandardQueryCache:") { Expiration = TimeSpan.FromSeconds(9), RegionName = "RegionName:" } }; RedisCacheProvider.SetOptions(options); var dbFile = HttpContext.Current.Server.MapPath("~/App_Data/sample.db"); if (File.Exists(dbFile)) { File.Delete(dbFile); } var configuration = Fluently.Configure() .Database( SQLiteConfiguration.Standard.UsingFile(dbFile) ) .Mappings(m => m.FluentMappings.Add(typeof(BlogPostMapping))) .ExposeConfiguration(cfg => cfg.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true")) .Cache(c => c.UseQueryCache().UseSecondLevelCache().ProviderClass <RedisCacheProvider>()) .BuildConfiguration(); new SchemaExport(configuration).Create(false, true); SessionFactory = configuration.BuildSessionFactory(); }
/// <summary> /// 构造器 /// </summary> public SessionManager(Assembly[] assemblies) { //通过Mapping by code加载映射 var mapper = new ModelMapper(); foreach (var assembly in assemblies) { try { mapper.AddMappings(assembly.GetExportedTypes()); } catch (Exception e) { //有些程序集里不包含NH配置信息,会抛异常,捕获但不处理 } } //This will write all the XML into the bin/mappings folder if (HttpContext.Current == null) { mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping(); } var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities(); var configure = new Configuration(); configure.Configure(); configure.AddDeserializedMapping(hbmMapping, ""); configure.CurrentSessionContext <WebSessionContext>(); //设置NHibernate使用Redis缓存 //https://github.com/TheCloudlessSky/NHibernate.Caches.Redis var connectionMultiplexer = DiContainer.Resolve <ConnectionMultiplexer>(); RedisCacheProvider.SetConnectionMultiplexer(connectionMultiplexer); //设置Redis的序列化器 var options = new RedisCacheProviderOptions() { Serializer = new NhJsonCacheSerializer(), KeyPrefix = "Sopcce" }; RedisCacheProvider.SetOptions(options); //设置NHibernate在表结构变化时自动更新缓存 //https://github.com/TheCloudlessSky/NHibernate.Cache.DynamicCacheBuster new CacheBuster().AppendVersionToCacheRegionNames(configure); _sessionFactory = configure.BuildSessionFactory(); }
protected override ISessionFactory CreateSessionFactory(bool enableCache, string cacheProvider) { if (enableCache) { // Or use your IoC container to wire this up. var connectionMultiplexer = ConnectionMultiplexer.Connect(RedisConnectionString); RedisCacheProvider.SetConnectionMultiplexer(connectionMultiplexer); var options = new RedisCacheProviderOptions() { Serializer = new NhJsonCacheSerializer(), }; RedisCacheProvider.SetOptions(options); } var sessionFactory = base.CreateSessionFactory(enableCache, cacheProvider); return(sessionFactory); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); var connectionMultiplexer = ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true"); connectionMultiplexer.GetServer("localhost", 6379).FlushAllDatabases(); RedisCacheProvider.SetConnectionMultiplexer(connectionMultiplexer); RedisCacheProvider.SetOptions(new RedisCacheProviderOptions() { Serializer = new NHibernateJsonCacheSerializer(), }); var dbFile = HttpContext.Current.Server.MapPath("~/App_Data/sample.db"); if (File.Exists(dbFile)) { File.Delete(dbFile); } var configuration = Fluently.Configure() .Database( SQLiteConfiguration.Standard.UsingFile(dbFile) ) .Mappings(m => m.FluentMappings.Add(typeof(BlogPostMapping))) .ExposeConfiguration(cfg => cfg.SetProperty(Cfg.Environment.GenerateStatistics, "true")) .Cache(c => c.UseQueryCache().UseSecondLevelCache().ProviderClass <RedisCacheProvider>()) .BuildConfiguration(); new SchemaExport(configuration).Create(false, true); SessionFactory = configuration.BuildSessionFactory(); }