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();
        }
        void the_copy_constructor_copies_all_event_handlers()
        {
            var sut = new RedisCacheProviderOptions();
            var order = new List<string>();
            sut.Exception += (s, e) => order.Add("a");
            sut.Exception += (s, e) => order.Add("b");
            sut.Exception += (s, e) => order.Add("c");

            var clone = sut.ShallowCloneAndValidate();
            clone.OnException(null, new ExceptionEventArgs("foo", RedisCacheMethod.Unknown, new Exception()));

            Assert.Equal(new[] { "a", "b", "c" }, order);
        }
        public void The_copy_constructor_copies_all_event_handlers()
        {
            var sut   = new RedisCacheProviderOptions();
            var order = new List <string>();

            sut.Exception += (s, e) => order.Add("a");
            sut.Exception += (s, e) => order.Add("b");
            sut.Exception += (s, e) => order.Add("c");

            var clone = sut.ShallowCloneAndValidate();

            clone.OnException(null, new ExceptionEventArgs("foo", RedisCacheMethod.Unknown, new Exception()));

            Assert.AreEqual(new[] { "a", "b", "c" }, order);
        }
        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);
        }
 public RedisCacheTests()
 {
     options = CreateTestProviderOptions();
 }
 public RequestRecoveryRedisCache(string regionName, IDictionary <string, string> properties, RedisCacheElement element, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
     : base(regionName, properties, element, connectionMultiplexer, options)
 {
 }
 public RedisCacheTests()
 {
     options = CreateTestProviderOptions();
 }
        protected override RedisCache BuildCache(string regionName, IDictionary <string, string> properties, RedisCacheElement configElement, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            var cache = new MyRedisCache(regionName, properties, configElement, connectionMultiplexer, options);

            return(cache);
        }
Example #10
0
        protected override RedisCache BuildCache(string regionName, IDictionary <string, string> properties, RedisCacheElement configElement, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            options.OnException = (e) =>
            {
                var httpContext = HttpContext.Current;
                if (httpContext != null)
                {
                    httpContext.Items[RequestRecoveryRedisCache.SkipNHibernateCacheKey] = true;
                }
            };

            return(new RequestRecoveryRedisCache(regionName, properties, configElement, connectionMultiplexer, options));
        }