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 Entry(string filePath, FileLruCache <T> cache, int slot, Func <string, T> factory)
            {
                this.cache    = cache;
                this.factory  = factory;
                this.filePath = filePath;
                Slot          = slot;
                lazyValue     = new Lazy <T>(() => factory(filePath), LazyThreadSafetyMode.ExecutionAndPublication);

                watcher = new FileSystemWatcher(
                    Path.GetDirectoryName(this.filePath),
                    Path.GetFileName(this.filePath));
                watcher.Changed            += OnChanged;
                watcher.EnableRaisingEvents = true;
            }
Esempio n. 6
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. 7
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. 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 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);
        }
Esempio n. 10
0
        public SolutionTraceSettingsContext(ISolutionBrowser solutionBrowser = null)
        {
            this.solutionBrowser = solutionBrowser;

            ProjectsInSolution  = FindProjects();
            ManifestsInSolution = ThreadingExtensions.CreateAsyncLazy(FindManifests);

            manifestCache = new FileLruCache <EventManifest>(
                10, path => {
                var diags = new DiagnosticCollector();

                var parser   = EventManifestParser.CreateWithWinmeta(diags);
                var manifest = parser.ParseManifest(path);
                if (manifest == null || diags.Diagnostics.Count != 0)
                {
                    throw new Exception(
                        string.Join("\r\n", diags.Diagnostics.Select(x => x.Message)));
                }

                return(manifest);
            });
        }
Esempio n. 11
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. 12
0
        /// <summary>
        /// Stores downloaded image to database LruCache.
        /// </summary>
        /// <param name="key">Image key, it can be URL or Path.</param>
        /// <param name="fullImage">Full Bitmap.</param>
        /// <param name="previewImage">Preview Bitmap.</param>
        /// <param name="addData">Additional data. Note: it is not used in this time.</param>
        public void InsertImageToCache(string key, Bitmap fullImage, Bitmap previewImage, long addData)
        {
            var cacheRecord = new FileLruCache(key, ImageUtils.Instance.TransformBitmapToByteArray(fullImage), ImageUtils.Instance.TransformBitmapToByteArray(previewImage), addData);

            IWantToDatabase.InsertFileLruCache(cacheRecord);
        }
Esempio n. 13
0
 public static long InsertFileLruCache(FileLruCache fileLruCache)
 {
     return(InsertItem(fileLruCache));
 }