public void ObjectRetrievedFromFirstLevelCacheShouldNotBeClonedIfTheyAreFromASpecificNameSpace() { var namespaceCloningProvider = new NamespacesBasedCloningProvider(new [] { "System" }); var cache = new SquirrelCache(_config, namespaceCloningProvider, _serializationProvider); var testObject2 = EqualityComparer <string> .Default; var serializedTestObject2 = KrakenSerializer.Serialize(testObject2); var key1 = RandomKey(); var key2 = RandomKey(); cache.Set(key1, _serializedTestObject); cache.Set(key2, serializedTestObject2); var obj1_1 = cache.Get <object>(key1).Value; var obj1_2 = cache.Get <object>(key1).Value; Assert.NotNull(obj1_1); Assert.True(ReferenceEquals(obj1_1, obj1_2)); var obj2_1 = cache.Get <object>(key2).Value; var obj2_2 = cache.Get <object>(key2).Value; Assert.NotNull(obj2_1); Assert.False(ReferenceEquals(obj2_1, obj2_2)); }
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); }
public void UsingACustomSerializationProviderShouldWork() { var key = Guid.NewGuid().ToString(); var sp = new BinaryFormatterSerializationProvider(); var objBytes = sp.Serialize(_testObject); var cacheRepository = new SquirrelCache(_config, _noCloningProvider, sp); cacheRepository.Set(key, objBytes); Assert.Equal(_testObject.Value, cacheRepository.Get <CachingTestClass>(key).Value.Value); }
public void ObjectRetrievedFromFirstLevelCacheShouldAlwaysBeCloned() { var cache = new SquirrelCache(_config, _alwaysCloningProvider, _serializationProvider); var key = RandomKey(); cache.Set(key, _serializedTestObject); var obj1 = cache.Get <object>(key).Value; var obj2 = cache.Get <object>(key).Value; Assert.NotNull(obj1); Assert.False(ReferenceEquals(obj1, obj2)); }
public void AddingItemToCacheWithoutTtlShouldStoreIt() { var key = Guid.NewGuid().ToString(); _cacheRepository.Set(key, _serializedTestObject); Assert.Equal(_testObject.Value, _cacheRepository.Get <CachingTestClass>(key).Value.Value); }