Ejemplo n.º 1
0
        public void CreatingASquirrelCacheWithInvalidMemorySizeValueShoudThrowException()
        {
            var config = new FirstLevelCacheConfig();

            config.AbsoluteExpiration = TimeSpan.FromSeconds(5);
            config.MaximumMemorySize  = 0;
            config.TimerInterval      = TimeSpan.FromSeconds(1);
            Assert.Throws <ArgumentException>(() => new SquirrelCache(config, _noCloningProvider, _serializationProvider));
        }
Ejemplo n.º 2
0
        public void SquirrelCacheShouldBeThreadSafe()
        {
            var config = new FirstLevelCacheConfig();

            config.AbsoluteExpiration = TimeSpan.FromSeconds(3600);
            config.MaximumMemorySize  = 100000000;
            config.TimerInterval      = TimeSpan.FromSeconds(5);
            var cacheRepository = new SquirrelCache(config, _noCloningProvider, _serializationProvider);

            // seed cache
            var seededItems       = 100000;
            int numberOfMod3Items = Enumerable.Range(0, seededItems).Count(x => x % 3 == 0);

            Parallel.For(0,
                         seededItems,
                         i =>
            {
                cacheRepository.Set("cache_" + i,
                                    _serializedTestObject);
            });

            Assert.Equal(seededItems,
                         cacheRepository.NumberOfObjects);
            Assert.Equal(seededItems * _serializedTestObject.Length,
                         cacheRepository.EstimatedMemorySize);

            Parallel.For(0,
                         seededItems,
                         i =>
            {
                if (i % 3 == 0)
                {
                    cacheRepository.Remove("cache_" + i);
                }
                else
                {
                    cacheRepository.Set("cache_" + i,
                                        _serializedTestObject);
                }
            });

            WaitFor(10);

            Assert.Equal(seededItems - numberOfMod3Items,
                         cacheRepository.NumberOfObjects);
            Assert.Equal((seededItems - numberOfMod3Items) * _serializedTestObject.Length,
                         cacheRepository.EstimatedMemorySize);
        }