Esempio n. 1
0
        public void EvictsOldestItem2()
        {
            var cache = new FileLruCache <DisposableItem>(2, x => new DisposableItem(x));
            var arg1  = Path.GetTempFileName();
            var arg2  = Path.GetTempFileName();
            var arg3  = Path.GetTempFileName();

            var result1 = cache.Get(arg1);
            var result2 = cache.Get(arg2);

            Thread.Sleep(20);               // Sleep a bit to account for timing resolution.
            var result12 = cache.Get(arg1); // Touch

            var result3 = cache.Get(arg3);

            Assert.False(result1.Disposed);
            Assert.True(result2.Disposed);
            Assert.False(result3.Disposed);
            Assert.Same(result1, result12);
            Assert.NotSame(result1, result2);
            Assert.NotSame(result1, result3);
            Assert.NotSame(result2, result3);
            Assert.Equal(arg1, result1.Path);
            Assert.Equal(arg2, result2.Path);
            Assert.Equal(arg3, result3.Path);
        }
Esempio n. 2
0
        public void GetCachedItem()
        {
            var cache = new FileLruCache <Item>(2, x => new Item(x));
            var arg   = Path.GetTempFileName();

            var result1 = cache.Get(arg);
            var result2 = cache.Get(arg);

            Assert.Same(result1, result2);
            Assert.Equal(arg, result1.Path);
            Assert.Equal(arg, result2.Path);
        }
Esempio n. 3
0
        public void RemoveCachedItem()
        {
            var cache = new FileLruCache <Item>(2, x => new Item(x));
            var arg   = Path.GetTempFileName();

            var result1 = cache.Get(arg);
            var removed = cache.Remove(arg);
            var result2 = cache.Get(arg);

            Assert.True(removed);
            Assert.NotSame(result1, result2);
            Assert.Equal(arg, result1.Path);
            Assert.Equal(arg, result2.Path);
        }
Esempio n. 4
0
        public void Dispose()
        {
            var cache = new FileLruCache <DisposableItem>(2, x => new DisposableItem(x));
            var arg1  = Path.GetTempFileName();
            var arg2  = Path.GetTempFileName();

            var result1 = cache.Get(arg1);
            var result2 = cache.Get(arg2);

            cache.Dispose();

            Assert.True(result1.Disposed);
            Assert.True(result2.Disposed);
        }
Esempio n. 5
0
        public void RemoveUncachedItem()
        {
            var cache = new FileLruCache <Item>(2, x => new Item(x));
            var arg1  = Path.GetTempFileName();
            var arg2  = Path.GetTempFileName();

            var result1 = cache.Get(arg1);
            var removed = cache.Remove(arg2);
            var result2 = cache.Get(arg1);

            Assert.False(removed);
            Assert.Same(result1, result2);
            Assert.Equal(arg1, result1.Path);
            Assert.Equal(arg1, result2.Path);
        }
Esempio n. 6
0
        public void Clear()
        {
            var cache = new FileLruCache <Item>(2, x => new Item(x));
            var arg1  = Path.GetTempFileName();
            var arg2  = Path.GetTempFileName();

            var result1 = cache.Get(arg1);
            var result2 = cache.Get(arg2);

            cache.Clear();
            var result12 = cache.Get(arg1);
            var result22 = cache.Get(arg2);

            Assert.NotSame(result1, result12);
            Assert.NotSame(result2, result22);
        }
Esempio n. 7
0
        public async Task <EventManifest> GetManifest(string manifestFile)
        {
            if (string.IsNullOrEmpty(manifestFile) || !File.Exists(manifestFile))
            {
                return(new EventManifest());
            }

            return(await Task.Run(() => manifestCache.Get(manifestFile)));
        }
Esempio n. 8
0
        public void EvictsChangedItem()
        {
            var cache = new FileLruCache <DisposableItem>(2, x => new DisposableItem(x, File.ReadAllText(x)));
            var arg1  = Path.GetTempFileName();

            File.WriteAllText(arg1, "foo");

            var result1 = cache.Get(arg1);

            File.WriteAllText(arg1, "bar"); // Change and evict
            Thread.Sleep(20);               // Sleep a bit to account for timing resolution.

            var result2 = cache.Get(arg1);  // Re-get

            Assert.True(result1.Disposed);
            Assert.Equal("foo", result1.Value);
            Assert.False(result2.Disposed);
            Assert.Equal("bar", result2.Value);
        }
Esempio n. 9
0
        public void EvictsOldestItem()
        {
            var cache = new FileLruCache <DisposableItem>(2, x => new DisposableItem(x));
            var arg1  = Path.GetTempFileName();
            var arg2  = Path.GetTempFileName();
            var arg3  = Path.GetTempFileName();

            var result1 = cache.Get(arg1);
            var result2 = cache.Get(arg2);

            var result3 = cache.Get(arg3);

            Assert.True(result1.Disposed);
            Assert.False(result2.Disposed);
            Assert.False(result3.Disposed);
            Assert.NotSame(result1, result2);
            Assert.NotSame(result1, result3);
            Assert.NotSame(result2, result3);
            Assert.Equal(arg1, result1.Path);
            Assert.Equal(arg2, result2.Path);
            Assert.Equal(arg3, result3.Path);
        }
Esempio n. 10
0
        public void GetUncachedItem()
        {
            string factoryArg    = null;
            int    factoryCalled = 0;
            var    item          = new Item("foo");

            Item Factory(string x)
            {
                factoryArg = x;
                ++factoryCalled;
                return(item);
            }

            var cache  = new FileLruCache <Item>(2, Factory);
            var arg    = Path.GetTempFileName();
            var result = cache.Get(arg);

            Assert.Same(item, result);
            Assert.Equal(arg, factoryArg);
            Assert.Equal(1, factoryCalled);
        }