Пример #1
0
        public void OverwriteWithAbsoluteExpirationTest()
        {
            var key = "OverwriteWithAbsoluteExpirationTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate <Guid>(key, () => val);

            Assert.AreEqual(result, val);

            var exist = cacheProvider.TryGet <Guid>(key, out val);

            Assert.IsTrue(exist);

            var val2 = Guid.NewGuid();

            cacheProvider.Overwrite <Guid>(key, val2, DateTime.UtcNow.AddSeconds(4D));

            Thread.Sleep(TimeSpan.FromSeconds(8D));
            Guid val3;

            exist = cacheProvider.TryGet <Guid>(key, out val3);
            Assert.IsFalse(exist);
            Assert.AreEqual(val3, Guid.Empty);
        }
Пример #2
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();
 }
        public void TryGetTest() {
            var key = "TryGetTest";
            Guid val;

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var exist = cacheProvider.TryGet<Guid>(key, out val);
            Assert.IsFalse(exist);
            Assert.AreEqual(val, Guid.Empty);

            var val2 = Guid.NewGuid();
            cacheProvider.Overwrite(key, val2);
            exist = cacheProvider.TryGet<Guid>(key, out val);
            Assert.IsTrue(exist);
            Assert.AreEqual(val, val2);
        }
        public void GetOrCreateWithslidingExpirationTest() {
            var key = "GetOrCreateWithslidingExpirationTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate<Guid>(key, () => val, TimeSpan.FromSeconds(4D));
            Assert.AreEqual(result, val);

            var exist = cacheProvider.TryGet<Guid>(key, out val);
            Assert.IsTrue(exist);
            Assert.AreEqual(result, val);

            Thread.Sleep(TimeSpan.FromSeconds(8D));
            exist = cacheProvider.TryGet<Guid>(key, out val);
            Assert.IsFalse(exist);
            Assert.AreEqual(val, Guid.Empty);
        }
Пример #5
0
        public void TryGetTest()
        {
            var  key = "TryGetTest";
            Guid val;

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var exist = cacheProvider.TryGet <Guid>(key, out val);

            Assert.IsFalse(exist);
            Assert.AreEqual(val, Guid.Empty);

            var val2 = Guid.NewGuid();

            cacheProvider.Overwrite(key, val2);
            exist = cacheProvider.TryGet <Guid>(key, out val);
            Assert.IsTrue(exist);
            Assert.AreEqual(val, val2);
        }
Пример #6
0
        public void GetOrCreateWithslidingExpirationTest()
        {
            var key = "GetOrCreateWithslidingExpirationTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate <Guid>(key, () => val, TimeSpan.FromSeconds(4D));

            Assert.AreEqual(result, val);

            var exist = cacheProvider.TryGet <Guid>(key, out val);

            Assert.IsTrue(exist);
            Assert.AreEqual(result, val);

            Thread.Sleep(TimeSpan.FromSeconds(8D));
            exist = cacheProvider.TryGet <Guid>(key, out val);
            Assert.IsFalse(exist);
            Assert.AreEqual(val, Guid.Empty);
        }
Пример #7
0
        public void OverwriteTest()
        {
            var key = "OverwriteTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate <Guid>(key, () => val);

            Assert.AreEqual(result, val);

            var val2 = Guid.NewGuid();

            cacheProvider.Overwrite <Guid>(key, val2);

            Guid val3;
            var  exist = cacheProvider.TryGet <Guid>(key, out val3);

            Assert.IsTrue(exist);
            Assert.AreEqual(val3, val2);
        }
Пример #8
0
        public void ExpireTest()
        {
            var key = "ExpireTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate <Guid>(key, () => val);

            Assert.AreEqual(result, val);

            var exist = cacheProvider.TryGet <Guid>(key, out val);

            Assert.IsTrue(exist);

            cacheProvider.Expire(key);
            Guid val2;

            exist = cacheProvider.TryGet <Guid>(key, out val2);
            Assert.IsFalse(exist);
            Assert.AreEqual(val2, Guid.Empty);
        }
        public void GetOrCreateTest() {
            var key = "GetOrCreateTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate<Guid>(key, () => val);
            Assert.AreEqual(result, val);

            {
                var exist = cacheProvider.TryGet<Guid>(key, out val);
                Assert.IsTrue(exist);
                Assert.AreEqual(result, val);
            }

            {
                var result2 = cacheProvider.GetOrCreate<Guid>(key, () => {
                    Assert.Fail();
                    return Guid.NewGuid();
                });
                Assert.AreEqual(result2, val);
            }
        }
Пример #10
0
        public void GetOrCreateTest()
        {
            var key = "GetOrCreateTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate <Guid>(key, () => val);

            Assert.AreEqual(result, val);

            {
                var exist = cacheProvider.TryGet <Guid>(key, out val);
                Assert.IsTrue(exist);
                Assert.AreEqual(result, val);
            }

            {
                var result2 = cacheProvider.GetOrCreate <Guid>(key, () => {
                    Assert.Fail();
                    return(Guid.NewGuid());
                });
                Assert.AreEqual(result2, val);
            }
        }
Пример #11
0
        /// <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;
                }
            }
        }
        public void OverwriteTest() {
            var key = "OverwriteTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate<Guid>(key, () => val);
            Assert.AreEqual(result, val);

            var val2 = Guid.NewGuid();
            cacheProvider.Overwrite<Guid>(key, val2);

            Guid val3;
            var exist = cacheProvider.TryGet<Guid>(key, out val3);
            Assert.IsTrue(exist);
            Assert.AreEqual(val3, val2);
        }
        public void ExpireTest() {
            var key = "ExpireTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate<Guid>(key, () => val);
            Assert.AreEqual(result, val);

            var exist = cacheProvider.TryGet<Guid>(key, out val);
            Assert.IsTrue(exist);

            cacheProvider.Expire(key);
            Guid val2;
            exist = cacheProvider.TryGet<Guid>(key, out val2);
            Assert.IsFalse(exist);
            Assert.AreEqual(val2, Guid.Empty);
        }
        public void OverwriteWithAbsoluteExpirationTest() {
            var key = "OverwriteWithAbsoluteExpirationTest";
            var val = Guid.NewGuid();

            IHttpRuntimeCacheProvider cacheProvider = new MongoCacheProvider();
            var result = cacheProvider.GetOrCreate<Guid>(key, () => val);
            Assert.AreEqual(result, val);

            var exist = cacheProvider.TryGet<Guid>(key, out val);
            Assert.IsTrue(exist);

            var val2 = Guid.NewGuid();
            cacheProvider.Overwrite<Guid>(key, val2, DateTime.UtcNow.AddSeconds(4D));

            Thread.Sleep(TimeSpan.FromSeconds(8D));
            Guid val3;
            exist = cacheProvider.TryGet<Guid>(key, out val3);
            Assert.IsFalse(exist);
            Assert.AreEqual(val3, Guid.Empty);

        }
Пример #15
0
 public void TestInitialize()
 {
     _cacheProvider = new MongoCacheProvider();
     Task.FromResult(_cacheProvider.RemoveAll());
 }