private void DoTest(StoreProvider provider) { // save bytes as new file provider.Save("test.txt", File.ReadAllBytes(TestFilePath)); // save stream as new file using (var fs = new FileStream(TestFilePath, FileMode.Open, FileAccess.Read)) { provider.Save("folder/folder2/test2.txt", fs); } // file exists Assert.IsTrue(provider.Exists("test.txt")); Assert.IsTrue(provider.Exists("folder/folder2/test2.txt")); Assert.IsFalse(provider.Exists("non-existing-file.txt")); byte[] bytes = null; Stream s = null; try { // load bytes Assert.IsTrue(provider.Load("test.txt", out bytes)); // load stream Assert.IsTrue(provider.Load("folder/folder2/test2.txt", out s)); // compare for (var i = 0; i < bytes.Length; i++) { Assert.AreEqual(s.ReadByte(), bytes[i]); } Assert.AreEqual(s.Position, s.Length); } finally { s.Dispose(); } // overwrite file and read it to detect whether the changes were really saved var newData = new byte[] { 1, 2, 3, 4, 5 }; provider.Save("test.txt", newData); byte[] newData2; provider.Load("test.txt", out newData2); for (int i = 0; i < newData.Length; i++) { Assert.AreEqual(newData[i], newData2[i]); } Assert.AreEqual(newData.Length, newData2.Length); // delete files provider.Delete("test.txt"); provider.Delete("folder/folder2/test2.txt"); // file exists Assert.IsFalse(provider.Exists("test.txt")); Assert.IsFalse(provider.Exists("folder/folder2/test2.txt")); }
/// <summary> /// Clean up the cache by deleting any cached items that have an expiration time /// older than the specified time. This is a relatively expensive operation so should be /// called sparingly. /// </summary> /// <param name="maximumExpirationTime">The maximum expiration time to delete. In other words, this call will delete /// any cache items who's expiration is older than this value.</param> /// <param name="complete">An Action that will be called when the cleanup operation has completed.</param> public void Cleanup(DateTime maximumExpirationTime, Action complete) { PriorityQueue.AddStorageWorkItem( () => { int retries = 3; while (retries-- > 0) { try { // get the list of items. // var itemsToCleanup = from item in StoreProvider.GetItems() where item.ExpirationTime <= maximumExpirationTime select item; // we snap the enumerable to an array to guard against any provider // implementations that might have returned an enumerator that would be affected // by the delete operation. // foreach (var item in itemsToCleanup.ToArray()) { StoreProvider.Delete(item); } } catch (Exception ex) { Debug.WriteLine("Exception trying to clean up (attempt {0}): {1}", 3 - retries, ex); // someetimes exceptions come out of the isostore stack trying to get the directory names, // so we just wait a bit and try again. // Thread.Sleep(50); } } // BUGBUG: What do to if this didn't ever actually complete succesfully. if (complete != null) { PriorityQueue.AddUiWorkItem(() => { complete(); } ); } } ); }