示例#1
0
 public void TestInitialize()
 {
     _cacheProvider = new MultiCacheProvider();
     _mongoCache    = new MongoCacheProvider();
     _memoryCache   = new MemoryCacheProvider();
     _mongoCache.Initialize("CacheMulti", _enabledSettings);
     _memoryCache.Initialize("CacheMulti", _enabledSettings);
     Task.FromResult(_mongoCache.RemoveAll());
     MongoUtilities.GetMongoDataBase(MongoUtilities.GetMongoDatabaseString(HostName, Port, DatabaseName)).Drop();
 }
        /// <summary>
        ///     Initialize from config
        /// </summary>
        /// <param name="name"></param>
        /// <param name="config">Config properties</param>
        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);

            _isEnabled = true;
            var enabled = config["enable"];

            if (enabled == null)
            {
                _isEnabled = true;
            }
            else
            {
                bool.TryParse(config["enable"], out _isEnabled);
            }

            var timeout = config["timeout"];

            if (string.IsNullOrEmpty(timeout))
            {
                timeout = "20";
            }

            var cacheExpirationTime = 60;

            if (!int.TryParse(timeout, out cacheExpirationTime))
            {
                throw new ConfigurationErrorsException("invalid timeout value");
            }

            var providers = config["providers"];

            if (providers == null)
            {
                throw new ConfigurationErrorsException("Missing providers list.. example memory,mongo");
            }

            var providerOptions = providers.Split(',');

            foreach (var provider in providerOptions.Select(c => c.Trim()).Where(provider => !string.IsNullOrWhiteSpace(provider)))
            {
                NameValueCollection valueCollection = config;
                int timeSlice = providerOptions.Count() - CacheProviders.Count;
                if (timeSlice < 1)
                {
                    timeSlice = 1;
                }

                var providerItem    = provider.Split(':');
                var providerTimeout = (cacheExpirationTime / timeSlice);
                if (providerItem.Length > 1)
                {
                    int.TryParse(providerItem[1], out providerTimeout);
                }

                valueCollection["timeout"] = providerTimeout.ToString(CultureInfo.InvariantCulture);

                switch (providerItem[0].ToLower())
                {
                case "memorycacheprovider":
                    var memoryProvider = new MemoryCacheProvider();
                    memoryProvider.Initialize(name, valueCollection);
                    CacheProviders.Add(memoryProvider);
                    break;

                case "mongocacheprovider":
                    var mongoProvider = new MongoCacheProvider();
                    mongoProvider.Initialize(name, valueCollection);
                    CacheProviders.Add(mongoProvider);
                    break;
                }
            }
        }
示例#3
0
        public async Task GetItemThatDoesNotExistTest()
        {
            _cacheProvider.Initialize("test", _enabledSettings);
            const string key = "TestKey";

            var results = await _cacheProvider.Get(key, "FirstRegion");

            Assert.IsNull(results);
        }