public void TransactionExceedingTimeoutTest( ) { var testCache = new TestCache( ); using (var scope = new TransactionScope(TransactionScopeOption.Required)) { // Create a notification manager with a time out interval of 1 seconds. var cacheManager = new TransactionEventNotificationManager(testCache, 0); // Add an items to the cache while inside a transaction testCache.AddItem("A"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("B"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("C"); cacheManager.EnlistTransaction(Transaction.Current); scope.Complete( ); // As the transaction exceeded the timeout, its data will not // be committed } Assert.IsFalse(testCache.Contains("A")); Assert.IsFalse(testCache.Contains("B")); Assert.IsFalse(testCache.Contains("C")); }
public void EnlistRolledbackTransactionTest( ) { var testCache = new TestCache( ); using (new TransactionScope(TransactionScopeOption.Required)) { var cacheManager = new TransactionEventNotificationManager(testCache); // Add some items to the cache while inside a transaction testCache.AddItem("A"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("B"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("C"); cacheManager.EnlistTransaction(Transaction.Current); // Roll back transaction } Assert.IsFalse(testCache.Contains("A")); Assert.IsFalse(testCache.Contains("B")); Assert.IsFalse(testCache.Contains("C")); }
public void EnlistCommitTransactionTest( ) { var testCache = new TestCache( ); using (var scope = new TransactionScope(TransactionScopeOption.Required)) { var cacheManager = new TransactionEventNotificationManager(testCache); // Add some items to the cache while inside a transaction testCache.AddItem("A"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("B"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("C"); cacheManager.EnlistTransaction(Transaction.Current); // Commit the transaction scope.Complete( ); } Assert.IsTrue(testCache.Contains("A")); Assert.IsTrue(testCache.Contains("B")); Assert.IsTrue(testCache.Contains("C")); }
public void ClearTransactionEventNotifiersTest( ) { var testCache = new TestCache( ); using (var scope = new TransactionScope(TransactionScopeOption.Required)) { var cacheManager = new TransactionEventNotificationManager(testCache); // Add some items to the cache while inside a transaction testCache.AddItem("A"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("B"); cacheManager.EnlistTransaction(Transaction.Current); testCache.AddItem("C"); cacheManager.EnlistTransaction(Transaction.Current); // Clear the event notifiers cacheManager.ClearTransactionEventNotifiers( ); // Commit the transaction // as the. As the event notifiers have been removed // the items will not be commited scope.Complete( ); } Assert.IsFalse(testCache.Contains("A")); Assert.IsFalse(testCache.Contains("B")); Assert.IsFalse(testCache.Contains("C")); }
public void EnlistNullTransactionTest( ) { var testCache = new TestCache( ); var cacheManager = new TransactionEventNotificationManager(testCache); cacheManager.EnlistTransaction(null); }
/// <summary> /// Constructor that initializes both the internal cache and lru list. /// </summary> /// <remarks> /// The cache is transaction aware. Any objects that are added or retrieved /// from the cache during a transaction are removed if the transaction is rolled back. /// If the cache is used to cache database objects consider making the cache transaction aware. /// </remarks> /// <param name="cacheName"> /// Name of the cache. /// </param> /// <param name="publicCache"> /// The cache being wrapped. This cannot be null. /// </param> /// <param name="privateCacheFactory"> /// A function to create a new cache instance. Used to create a new cache for each transaction. /// This cannot be null. /// </param> /// <exception cref="ArgumentNullException"> /// No argument can be null. /// </exception> public TransactionAwareCache(string cacheName, ICache <TKey, TValue> publicCache, Func <string, ICache <TKey, TValue> > privateCacheFactory) { if (publicCache == null) { throw new ArgumentNullException(nameof(publicCache)); } if (privateCacheFactory == null) { throw new ArgumentNullException(nameof(privateCacheFactory)); } _cacheName = cacheName; _publicCache = publicCache; _privateCacheFactory = privateCacheFactory; _privateCaches = new ConcurrentDictionary <string, ICache <TKey, TValue> >(); _transactionEventNotificationManager = new TransactionEventNotificationManager(this); }