public static void GetSetValue_WhenInvokedTogetherUpdatingValue_HasEqualInputAndOutput() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "getsetvalue_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); var input = new byte[] { 1, 2, 3, 4 }; var updatedInput = new byte[] { 3, 4, 5, 6 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { cache.SetValue("asd", new MemoryStream(input)); cache.SetValue("asd", new MemoryStream(updatedInput)); var result = cache.GetValue("asd"); using (var reader = new BinaryReader(result)) { var resultBytes = reader.ReadBytes(4); var seqEqual = updatedInput.SequenceEqual(resultBytes); Assert.IsTrue(seqEqual); } } testDir.Delete(true); }
private static void Main() { var cacheDir = new DirectoryInfo(@"C:\Users\sjp\Downloads\tmp\cache"); var cachePolicy = new LruCachePolicy <string>(); const ulong maxSize = 1024L * 1024L * 1024L * 1024L; // 1GB using (var diskCache = new DiskCache <string>(cacheDir, cachePolicy, maxSize)) { diskCache.EntryAdded += (_, e) => Console.WriteLine($"Added: { e.Key }, { e.Size }"); using (var flacStream = File.OpenRead(@"C:\Users\sjp\Downloads\05. End Of Days.flac")) diskCache.SetValue("flacFile", flacStream); var containsTest = diskCache.ContainsKey("flacFile"); if (containsTest) { using (var outStr = diskCache.GetValue("flacFile")) using (var tmpFile = File.OpenWrite(@"C:\Users\sjp\Downloads\tmp.flac")) outStr.CopyTo(tmpFile); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(true); } }
public static void GetValue_WhenEmpty_ThrowsKeyNotFoundEx() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "getvalue_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); const ulong size = 123; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { Assert.Throws <KeyNotFoundException>(() => cache.GetValue("asd")); } testDir.Delete(true); }
public static void GetValue_WhenValueExpired_ThrowsKeyNotFoundException() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "getvalueexpired_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = new FixedTimespanCachePolicy <string>(TimeSpan.FromMilliseconds(1)); var input = new byte[] { 1, 2, 3, 4 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size, TimeSpan.FromMilliseconds(5))) { cache.SetValue("asd", new MemoryStream(input)); Task.Delay(100).Wait(); Assert.Throws <KeyNotFoundException>(() => cache.GetValue("asd")); } testDir.Delete(true); }