Пример #1
0
        public object Any(SetCacheProviderRequest request)
        {
            try
            {
                switch (request.provider)
                {
                    case "memcache":
                        var memcache = new MemcachedClientCache();
                        AppHost.Instance.Container.Register<ICacheClient>(memcache);
                        return new HttpResult("Cache Provider switched to MemCache.");

                    case "redis":
                        AppHost.Instance.Container.Register<ICacheClient>(c => c.Resolve<IRedisClientsManager>().GetCacheClient());
                        return new HttpResult("Cache Provider switched to Redis.");

                    case "aws":
                        var aws = new DynamoDbCacheClient("", "", Amazon.RegionEndpoint.APSoutheast1);
                        AppHost.Instance.Container.Register<ICacheClient>(aws);
                        return new HttpResult("Cache Provider switched to Amazon Web Service DynamoDb Cache Client.");

                    case "azure":
                        AppHost.Instance.Container.Register<ICacheClient>(new AzureCacheClient("default"));
                        return new HttpResult("Cache Provider switched to Microsoft Azure Cache Client.");

                    default:
                        AppHost.Instance.Container.Register<ICacheClient>(new MemoryCacheClient());
                        return new HttpResult("Cache Provider switched to In-Memory Cache Client.");
                }
            }
            catch
            {
                throw;
            }
        }
        public void Can_Store_Complex_Type()
        {
            var value = TestType.Create();

            var client = new MemcachedClientCache(TestConfig.MasterHosts);
            Assert.IsTrue(client.Set("asdf", value));
        }
        public object Any(SetCacheProviderRequest request)
        {
            try
            {
                switch (request.provider)
                {
                case "memcache":
                    var memcache = new MemcachedClientCache();
                    AppHost.Instance.Container.Register <ICacheClient>(memcache);
                    return(new HttpResult("Cache Provider switched to MemCache."));

                case "redis":
                    AppHost.Instance.Container.Register <ICacheClient>(c => c.Resolve <IRedisClientsManager>().GetCacheClient());
                    return(new HttpResult("Cache Provider switched to Redis."));

                case "aws":
                    var aws = new DynamoDbCacheClient("", "", Amazon.RegionEndpoint.APSoutheast1);
                    AppHost.Instance.Container.Register <ICacheClient>(aws);
                    return(new HttpResult("Cache Provider switched to Amazon Web Service DynamoDb Cache Client."));

                case "azure":
                    AppHost.Instance.Container.Register <ICacheClient>(new AzureCacheClient("default"));
                    return(new HttpResult("Cache Provider switched to Microsoft Azure Cache Client."));

                default:
                    AppHost.Instance.Container.Register <ICacheClient>(new MemoryCacheClient());
                    return(new HttpResult("Cache Provider switched to In-Memory Cache Client."));
                }
            }
            catch
            {
                throw;
            }
        }
Пример #4
0
        public void Memcached_GetSetIntValue_returns_missing_keys()
        {
            var client = new MemcachedClientCache(TestConfig.MasterHosts);

            AssertGetSetIntValue((IMemcachedClient)client);
            AssertGetSetIntValue((ICacheClient)client);
        }
Пример #5
0
 public MemcachedClientCache MemcachedClient()
 {
     //解决“Memcached黑色3秒事件”问题
     //Memcached的socket的连接时间大约为12小时间会自动回收断开断开后会有3秒内无法插入读取
     //当前时间是指在同一分钟内使用相同的客户端实例对象
     //如果下个时间点nextTime不在同一分钟内,则进行预热加载客户端实例对象
     var currentTime = DateTime.Now;
     var nextTime = currentTime.AddMinutes(10);
     var t = currentTime.Hour%_clientNum;
     var tt = nextTime.Hour%_clientNum;
     //var t = currentTime.Second % _clientNum;
     //var tt = nextTime.Second % _clientNum;
     _client = _clients[t];
     //目前每隔1分钟转换一个实体
     if (tt != _index)
     {
         //通过锁来控制客户端的释放
         lock (LockObject)
         {
             //用最小的性能代价-尽可能的保证线程安全
             if (tt != _index)
             {
                 _index = tt;
                 var t1 = (t + 1)%_clientNum;
                 //重新加载实体
                 _clients[t1].Dispose();
                 _clients[t1] = null;
                 _clients[t1] = LoadClient();
             }
         }
     }
     if (_client == null) SetMemcachedClient();
     return _client;
 }
Пример #6
0
        public void Can_Store_Complex_Type()
        {
            var value = TestType.Create();

            var client = new MemcachedClientCache(TestConfig.MasterHosts);

            Assert.IsTrue(client.Set("asdf", value));
        }
        public void Can_Store_And_Get_Complex_Type()
        {
            var value = TestType.Create();

            var client = new MemcachedClientCache(TestConfig.MasterHosts);
            Assert.IsTrue(client.Set("asdf", value));

            var target = client.Get<TestType>("asdf");
            Assert.AreEqual(TestType.Create(), target);
        }
Пример #8
0
        public void Can_Store_And_Get_Complex_Type()
        {
            var value = TestType.Create();

            var client = new MemcachedClientCache(TestConfig.MasterHosts);

            Assert.IsTrue(client.Set("asdf", value));

            var target = client.Get <TestType>("asdf");

            Assert.AreEqual(TestType.Create(), target);
        }
Пример #9
0
 private static void Example()
 {
     MemcachedClientCache c = new MemcachedClientCache(new[] { "10.48.251.81:11211" });
     c.Add("201606141340", "1", new TimeSpan(0, 10, 0));
     Stopwatch watch = Stopwatch.StartNew();
     for (int i = 0; i < 1000; i++)
     {
         var s = c.Set("201606141340", "1", new TimeSpan(0, 10, 0));
         if (!s)
         {
             Console.Write(s);
         }
     }
     watch.Stop();
     Console.WriteLine(watch.ElapsedMilliseconds);
 }
 /// <summary>
 /// 构造函数
 /// </summary>
 public MemcachedCacheProvider()
 {
     cache = new MemcachedClientCache();
     try
     {
         string path = WebHelper.GetConfigFilePath("memcachedcache.config");
         MemcachedCacheConfigInfo configInfo = (MemcachedCacheConfigInfo)XmlHelper.DeserializeFromXML(typeof(MemcachedCacheConfigInfo), path);
         if (configInfo != null)
         {
             cache = new MemcachedClientCache(configInfo.ServerList, configInfo.MinPoolSize, configInfo.MaxPoolSize, configInfo.ConnectionTimeOut, configInfo.DeadTimeOut);
         }
         else
         {
             cache = new MemcachedClientCache();
         }
     }
     catch
     {
         cache = new MemcachedClientCache();
     }
 }
		public void Memcached_GetSetIntValue_returns_missing_keys()
		{
			var client = new MemcachedClientCache(TestConfig.MasterHosts);
			AssertGetSetIntValue((IMemcachedClient)client);
			AssertGetSetIntValue((ICacheClient)client);
		}
 public void Can_create_Instance_using_ConfigurationSection()
 {
     var client = new MemcachedClientCache();
 }
Пример #13
0
        public CoreRegistry()
        {
            var config = Fluently.Configure()
                         .Database(PostgreSQLConfiguration.PostgreSQL82
                                   .ConnectionString(c => c.FromAppSetting("lender_db"))
                                   .DefaultSchema(ConfigurationManager.AppSettings["lender_db_schema"])
                                   )
                         .CurrentSessionContext <ThreadStaticSessionContext>()
                         .Mappings(m =>
                                   m.FluentMappings
                                   .AddFromAssemblyOf <UserMap>()
                                   .AddFromAssemblyOf <UserAuthPersistenceDto>()
                                   .AddFromAssemblyOf <ServiceStackUser>()
                                   )
                         .BuildConfiguration()
            ;

            For <Configuration>()
            .Singleton()
            .Use(config)
            ;

            For <ISessionFactory>()
            .Singleton()
            .Use(config.BuildSessionFactory())
            ;

            For <IUnitOfWork>()
            .HybridHttpOrThreadLocalScoped()
            .Use <UnitOfWork.UnitOfWork>()
            ;

            For <ISession>()
            .Use(c => c.GetInstance <IUnitOfWork>().CurrentSession)
            ;

            Scan(scanner =>
            {
                scanner.AssemblyContainingType <Request>();
                scanner.AssemblyContainingType <ServiceStackUser>();
                scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler <,>));
                scanner.ConnectImplementationsToTypesClosing(typeof(IAuthenticatedRequestHandler <,>));
                scanner.ConnectImplementationsToTypesClosing(typeof(BaseAuthenticatedRequestHandler <,>));
            });

            For <IAuthenticatedRequestHandler <AddUserItemRequest, BaseResponse> >()
            .Use <AddUserItemRequestHandler>()
            ;

            For <IAuthenticatedRequestHandler <AddOrganisationItemRequest, BaseResponse> >()
            .Use <AddItemRequestHandler <Organisation> >()
            ;

            For <IUserAuthRepository>()
            .AlwaysUnique()
            .Use <NHibernateUserAuthRepository>()
            ;

            For <IAuthProvider>()
            .AlwaysUnique()
            .Use <UnitOfWorkAuthProvider>()
            ;

            For <AuthService>()
            .AlwaysUnique()
            .Use <UnitOfWorkAuthService>()
            ;

            var cfg   = ConfigurationManager.GetSection("enyim.com/memcached") as MemcachedClientSection;
            var cache = new MemcachedClientCache(cfg);

            For <ICacheClient>()
            .Singleton()
            .Use(cache)
            //.Use<MemoryCacheClient>()
            ;

            For <ItemWebService>()
            .AlwaysUnique()
            .Use <ItemWebService>()
            ;
        }
Пример #14
0
 private void SetMemcachedClient()
 {
     _client = LoadClient();
 }
Пример #15
0
        public CoreRegistry()
        {
            var config = Fluently.Configure()
                .Database(PostgreSQLConfiguration.PostgreSQL82
                    .ConnectionString(c => c.FromAppSetting("lender_db"))
                    .DefaultSchema(ConfigurationManager.AppSettings["lender_db_schema"])
                )
                .CurrentSessionContext<ThreadStaticSessionContext>()
                .Mappings(m =>
                    m.FluentMappings
                        .AddFromAssemblyOf<UserMap>()
                        .AddFromAssemblyOf<UserAuthPersistenceDto>()
                        .AddFromAssemblyOf<ServiceStackUser>()
                )
                .BuildConfiguration()
                ;

            For<Configuration>()
                .Singleton()
                .Use(config)
                ;

            For<ISessionFactory>()
                .Singleton()
                .Use(config.BuildSessionFactory())
                ;

            For<IUnitOfWork>()
                .HybridHttpOrThreadLocalScoped()
                .Use<UnitOfWork.UnitOfWork>()
                ;

            For<ISession>()
                .Use(c => c.GetInstance<IUnitOfWork>().CurrentSession)
                ;

            Scan(scanner =>
            {
                scanner.AssemblyContainingType<Request>();
                scanner.AssemblyContainingType<ServiceStackUser>();
                scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
                scanner.ConnectImplementationsToTypesClosing(typeof(IAuthenticatedRequestHandler<,>));
                scanner.ConnectImplementationsToTypesClosing(typeof(BaseAuthenticatedRequestHandler<,>));
            });

            For<IAuthenticatedRequestHandler<AddUserItemRequest, BaseResponse>>()
                .Use<AddUserItemRequestHandler>()
                ;

            For<IAuthenticatedRequestHandler<AddOrganisationItemRequest, BaseResponse>>()
                .Use<AddItemRequestHandler<Organisation>>()
                ;

            For<IUserAuthRepository>()
                .AlwaysUnique()
                .Use<NHibernateUserAuthRepository>()
                ;

            For<IAuthProvider>()
                .AlwaysUnique()
                .Use<UnitOfWorkAuthProvider>()
                ;

            For<AuthService>()
                .AlwaysUnique()
                .Use<UnitOfWorkAuthService>()
                ;

            var cfg = ConfigurationManager.GetSection("enyim.com/memcached") as MemcachedClientSection;
            var cache = new MemcachedClientCache(cfg);

            For<ICacheClient>()
                .Singleton()
                .Use(cache)
                //.Use<MemoryCacheClient>()
                ;

            For<ItemWebService>()
                .AlwaysUnique()
                .Use<ItemWebService>()
                ;
        }
Пример #16
0
 public void Can_create_Instance_using_ConfigurationSection()
 {
     var client = new MemcachedClientCache();
 }