private async void GetImpl(string requestUrl, Action <string> onComplete) { var fileUrl = await Task.Run(() => { var key = Crypto.Sha256(requestUrl); var snapshot = cacheImpl.Get(key); return(snapshot?.UnsafeUrl); }); onComplete(fileUrl); }
public void RestoreBackupFile() { var creator = cache.Edit("k1"); creator.PutAt(0, "ABC"); creator.PutAt(1, "DE"); creator.Commit(); cache.Dispose(); if (journalBackupFile.Exists) { journalBackupFile.Delete(); } File.Move(journalFile.FullName, journalBackupFile.FullName); Assert.That(journalFile.Exists, Is.False); cache = DiskLruCache.Open(directory, AppVersion, 2, int.MaxValue); var snapshot = cache.Get("k1"); Assert.That(snapshot, Is.Not.Null); Assert.That(snapshot.StringAt(0), Is.EqualTo("ABC")); Assert.That(snapshot.LengthAt(0), Is.EqualTo(3)); Assert.That(snapshot.StringAt(1), Is.EqualTo("DE")); Assert.That(snapshot.LengthAt(1), Is.EqualTo(2)); journalFile.Refresh(); Assert.That(journalBackupFile.Exists, Is.False); Assert.That(journalFile.Exists, Is.True); }
public void EvictionHonorsLruFromCurrentSession() { cache.Dispose(); cache = DiskLruCache.Open(directory, AppVersion, 2, 10); Set("a", "a", "a"); Set("b", "b", "b"); Set("c", "c", "c"); Set("d", "d", "d"); Set("e", "e", "e"); cache.Get("b").Dispose(); // 'B' is now least recently used. // Causing the size to grow to 12 should evict 'A'. Set("f", "f", "f"); // Causing the size to grow to 12 should evict 'C'. Set("g", "g", "g"); cache.Flush(); Assert.That(cache.Size, Is.EqualTo(10)); AssertAbsent("a"); AssertValue("b", "b", "b"); AssertAbsent("c"); AssertValue("d", "d", "d"); AssertValue("e", "e", "e"); AssertValue("f", "f", "f"); }
public void OpenWithDirtyKeyDeletesAllFilesForThatKey() { cache.Dispose(); var cleanFile0 = GetCleanFile("k1", 0); var cleanFile1 = GetCleanFile("k1", 1); var dirtyFile0 = GetDirtyFile("k1", 0); var dirtyFile1 = GetDirtyFile("k1", 1); WriteFile(cleanFile0, "A"); WriteFile(cleanFile1, "B"); WriteFile(dirtyFile0, "C"); WriteFile(dirtyFile1, "D"); // XXX: Original: createJournal("CLEAN k1 1 1", "DIRTY k1"); CreateJournal("CLEAN k1 1 1", "DIRTY k1"); cache = DiskLruCache.Open(directory, AppVersion, 2, int.MaxValue); Assert.That(cleanFile0.Exists, Is.False); Assert.That(cleanFile1.Exists, Is.False); Assert.That(dirtyFile0.Exists, Is.False); Assert.That(dirtyFile1.Exists, Is.False); Assert.That(cache.Get("k1"), Is.Null); }
public void OpenWithTooManyFileSizesClearsDirectory() { cache.Dispose(); GenerateSomeGarbageFiles(); CreateJournal("CLEAN k1 1 1 1"); cache = DiskLruCache.Open(directory, AppVersion, 2, int.MaxValue); AssertGarbageFilesAllDeleted(); Assert.That(cache.Get("k1"), Is.Null); }
public void EditSinceEvicted() { cache.Dispose(); cache = DiskLruCache.Open(directory, AppVersion, 2, 10); Set("a", "aa", "aaa"); // size 5 var snapshot = cache.Get("a"); Set("b", "bb", "bbb"); // size 5 Set("c", "cc", "ccc"); // size 5; will evict 'A' cache.Flush(); Assert.That(snapshot.Edit(), Is.Null); }
public void JournalFileIsPreferredOverBackupFile() { var creator = cache.Edit("k1"); creator.PutAt(0, "ABC"); creator.PutAt(1, "DE"); creator.Commit(); cache.Flush(); File.Copy(journalFile.FullName, journalBackupFile.FullName); creator = cache.Edit("k2"); creator.PutAt(0, "F"); creator.PutAt(1, "GH"); creator.Commit(); cache.Dispose(); Assert.That(journalFile.Exists, Is.True); Assert.That(journalBackupFile.Exists, Is.True); cache = DiskLruCache.Open(directory, AppVersion, 2, int.MaxValue); var snapshotA = cache.Get("k1"); Assert.That(snapshotA.StringAt(0), Is.EqualTo("ABC")); Assert.That(snapshotA.LengthAt(0), Is.EqualTo(3)); Assert.That(snapshotA.StringAt(1), Is.EqualTo("DE")); Assert.That(snapshotA.LengthAt(1), Is.EqualTo(2)); var snapshotB = cache.Get("k2"); Assert.That(snapshotB.StringAt(0), Is.EqualTo("F")); Assert.That(snapshotB.LengthAt(0), Is.EqualTo(1)); Assert.That(snapshotB.StringAt(1), Is.EqualTo("GH")); Assert.That(snapshotB.LengthAt(1), Is.EqualTo(2)); Assert.That(journalBackupFile.Exists, Is.True); Assert.That(journalFile.Exists, Is.True); }
public void ReadAndWriteEntryAcrossCacheOpenAndClose() { var editor = cache.Edit("k1"); editor.PutAt(0, "A"); editor.PutAt(1, "B"); editor.Commit(); cache.Dispose(); cache = DiskLruCache.Open(directory, AppVersion, 2, int.MaxValue); var snapshot = cache.Get("k1"); Assert.That(snapshot.StringAt(0), Is.EqualTo("A")); Assert.That(snapshot.LengthAt(0), Is.EqualTo(1)); Assert.That(snapshot.StringAt(1), Is.EqualTo("B")); Assert.That(snapshot.LengthAt(1), Is.EqualTo(1)); snapshot.Dispose(); }
public void OpenWithTruncatedLineDiscardsThatLine() { cache.Dispose(); WriteFile(GetCleanFile("k1", 0), "A"); WriteFile(GetCleanFile("k1", 1), "B"); using (var writer = new StreamWriter(journalFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))) { writer.Write(DiskLruCache.Magic + "\n" + DiskLruCache.Version1 + "\n100\n2\n\nCLEAN k1 1 1"); } cache = DiskLruCache.Open(directory, AppVersion, 2, int.MaxValue); Assert.That(cache.Get("k1"), Is.Null); // The journal is not corrupt when editing after a truncated line. Set("k1", "C", "D"); cache.Dispose(); cache = DiskLruCache.Open(directory, AppVersion, 2, int.MaxValue); AssertValue("k1", "C", "D"); }
public void WriteAndReadEntry() { var editor = cache.Edit("k1"); editor.PutAt(0, "ABC"); editor.PutAt(1, "DE"); Assert.That(editor.StringAt(0), Is.Null); Assert.That(editor.ReaderInstanceAt(0), Is.Null); Assert.That(editor.StringAt(1), Is.Null); Assert.That(editor.ReaderInstanceAt(1), Is.Null); editor.Commit(); var snapshot = cache.Get("k1"); Assert.That(snapshot.StringAt(0), Is.EqualTo("ABC")); Assert.That(snapshot.LengthAt(0), Is.EqualTo(3)); Assert.That(snapshot.StringAt(1), Is.EqualTo("DE")); Assert.That(snapshot.LengthAt(1), Is.EqualTo(2)); }