public void CleanCacheTest() { _cache = new FileCache("CleanCacheTest"); _cache.Add("foo", 1, DateTime.Now); // expires immediately _cache.Add("bar", 2, DateTime.Now + TimeSpan.FromDays(1)); // set to expire tomorrow _cache.Add("foo", 1, DateTime.Now, "region"); // expires immediately _cache.Add("bar", 2, DateTime.Now + TimeSpan.FromDays(1), "region"); // set to expire tomorrow var keys = _cache.GetKeys().ToList(); keys.Should().Contain("foo"); keys.Should().Contain("bar"); keys = _cache.GetKeys("region").ToList(); keys.Should().Contain("foo"); keys.Should().Contain("bar"); _cache.CleanCache(); keys = _cache.GetKeys().ToList(); keys.Should().NotContain("foo"); keys.Should().Contain("bar"); keys = _cache.GetKeys("region").ToList(); keys.Should().NotContain("foo"); keys.Should().Contain("bar"); }
public void MergeNotMergedTest() { using (var set = new FileCache()) { set.Add(10, new MemoryStream(new byte[11])); set.Add(30, new MemoryStream(new byte[11])); set.Add(50, new MemoryStream(new byte[11])); var ranges = set.GetRanges(); Assert.Equal(3, ranges.Count); var beginBefore = long.MinValue; var endBefore = long.MinValue; foreach (var rng in ranges) { Assert.True(beginBefore < rng.Start); Assert.True(endBefore < rng.End); beginBefore = rng.Start; endBefore = rng.End; } } using (var set = new FileCache()) { set.Add(50, new MemoryStream(new byte[11])); set.Add(30, new MemoryStream(new byte[11])); set.Add(10, new MemoryStream(new byte[11])); var ranges = set.GetRanges(); Assert.Equal(3, ranges.Count); var beginBefore = long.MinValue; var endBefore = long.MinValue; foreach (var rng in ranges) { Assert.True(beginBefore < rng.Start); Assert.True(endBefore < rng.End); beginBefore = rng.Start; endBefore = rng.End; } } using (var set = new FileCache()) { set.Add(30, new MemoryStream(new byte[11])); set.Add(50, new MemoryStream(new byte[11])); set.Add(10, new MemoryStream(new byte[11])); var ranges = set.GetRanges(); Assert.Equal(3, ranges.Count); var beginBefore = long.MinValue; var endBefore = long.MinValue; foreach (var rng in ranges) { Assert.True(beginBefore < rng.Start); Assert.True(endBefore < rng.End); beginBefore = rng.Start; endBefore = rng.End; } } }
public void CleanCacheTest() { FileCache target = new FileCache("CleanCacheTest"); target.Add("foo", 1, DateTime.Now); // expires immediately target.Add("bar", 2, DateTime.Now + TimeSpan.FromDays(1)); // set to expire tomorrow target.CleanCache(); Assert.IsNull(target["foo"]); Assert.IsNotNull(target["bar"]); }
public void CleanCacheTest() { _cache = new FileCache("CleanCacheTest"); _cache.Add("foo", 1, DateTime.Now); // expires immediately _cache.Add("bar", 2, DateTime.Now + TimeSpan.FromDays(1)); // set to expire tomorrow _cache.CleanCache(); _cache["foo"].Should().BeNull(); _cache["bar"].Should().NotBeNull(); }
public void LoadTilesAndFindTiles() { var threadCount = 500; var loadWork = new WorkTimer(threadCount); var findWork = new WorkTimer(threadCount); loadWork.TimeWork( i => new { TileIndex = new TileIndex(i % 10, i / 10, "1"), Image = new [] { (byte)i } }, args => _cache.Add(args.TileIndex, args.Image)); loadWork.WaitForTestsToComplete(); findWork.TimeWork(i => new TileIndex(i % 10, i / 10, "1"), index => _cache.Find(index)); findWork.WaitForTestsToComplete(); Console.WriteLine("Total FileCache.Add time is {0}ms", loadWork.TotalTime); Console.WriteLine("Average FileCache.Add time is {0}ms", loadWork.TotalTime / threadCount); Console.WriteLine("Max FileCache.Add time is {0}ms", loadWork.MaxTime); Console.WriteLine("Min FileCache.Add time is {0}ms", loadWork.MinTime); Console.WriteLine("Total FileCache.Find time is {0}ms", findWork.TotalTime); Console.WriteLine("Average FileCache.Find time is {0}ms", findWork.TotalTime / threadCount); Console.WriteLine("Max FileCache.Find time is {0}ms", findWork.MaxTime); Console.WriteLine("Min FileCache.Find time is {0}ms", findWork.MinTime); }
private void DownloadTile(object tile) { try { var parameters = (object[])tile; if (parameters.Length != 2) { throw new ArgumentException("Two parameters expected"); } var tileInfo = (TileInfo)parameters[0]; var doneEvent = (MultipleThreadResetEvent)parameters[1]; var url = tileProvider.Request.GetUri(tileInfo); var bytes = GetBitmap(url); if (bytes != null) { var name = fileCache.GetFileName(tileInfo.Index); fileCache.Add(tileInfo.Index, bytes); CreateRaster(tileInfo, name); } doneEvent.SetOne(); } catch (Exception ex) { MessageBox.Show("下载异常:" + ex.Message); } }
private void LocalFetchCompleted(object sender, FetchTileCompletedEventArgs e) { try { if (e.Error == null && e.Cancelled == false && _isThreadRunning && e.Image != null) { var ms = new MemoryStream(e.Image); _memoryCache.Add(e.TileInfo.Index, ms); _fileCache?.Add(e.TileInfo.Index, e.Image); } } catch (Exception ex) { e.Error = ex; } finally { _threadCount--; lock (_tilesInProgress) { if (_tilesInProgress.Contains(e.TileInfo.Index)) { _tilesInProgress.Remove(e.TileInfo.Index); } } _waitHandle.Set(); } DataChanged?.Invoke(this, new DataChangedEventArgs(e.Error, e.Cancelled, e.TileInfo) { Image = e.Image }); }
public void FlushTest() { _cache = new FileCache("FlushTest"); _cache.Add("Key1", "Value1", _cache.DefaultPolicy); _cache.Add("Key2", "Value2", DateTime.Now.AddDays(1), "Region1"); Thread.Sleep(500); //attempt flush _cache.Flush(DateTime.Now.AddDays(1)); Thread.Sleep(500); //check to see if size ends up at zero (expected result) _cache.GetCacheSize().Should().Be(0); }
private void DownloadTile(object tile) { var parameters = (object[])tile; if (parameters.Length != 2) { throw new ArgumentException("Two parameters expected"); } var tileInfo = (TileInfo)parameters[0]; var doneEvent = (MultipleThreadResetEvent)parameters[1]; var url = _tileProvider.Request.GetUri(tileInfo); var bytes = GetBitmap(url); try { if (bytes != null) { var name = _fileCache.GetFileName(tileInfo.Index); _fileCache.Add(tileInfo.Index, bytes); CreateRaster(tileInfo, name); } } catch (Exception ex) { LogManager.LogPath = AppDomain.CurrentDomain.BaseDirectory + "\\log\\"; LogManager.WriteLog("error", ex.Message); } doneEvent.SetOne(); }
public void Add(TileIndex index, byte[] image) { memoryCache.Add(index, image); if (CacheToDisk) { fileCache.Add(index, image); } }
/// <summary> /// Adds a value to the cache. /// </summary> /// <param name="item">The <c>CacheItem</c> to be added to the cache.</param> /// <returns> /// <c>true</c> if the key was not already added to the cache, <c>false</c> otherwise. /// </returns> protected override bool AddInternalPrepared(CacheItem <TCacheValue> item) { if (_cache.Contains(item.Key, item.Region)) { return(false); } var policy = GetPolicy(item); if (_serializer != null) { return(_cache.Add(item.Key, _serializer.SerializeCacheItem(item), policy, item.Region)); } else { return(_cache.Add(item.Key, item, policy, item.Region)); } }
public void AddThenGetTest(string addKey, string getKey) { var fileCache = new FileCache(); var expected = new object(); fileCache.Add(addKey, expected); Assert.IsTrue(fileCache.TryGetValue(getKey, out object?actual)); Assert.AreSame(expected, actual); }
public void Rename(string fromKey, string toKey) { var fileCache = new FileCache(); var expected = new object(); fileCache.Add(fromKey, expected); fileCache.ChangeKey(fromKey, toKey, overWrite: true); Assert.IsTrue(fileCache.TryGetValue(toKey, out object?actual)); Assert.AreSame(expected, actual); }
/// <summary> /// Method to add a tile image to the <see cref="FileCache"/> /// </summary> /// <param name="tileInfo">The tile info</param> /// <param name="bitmap">The tile image</param> protected void AddImageToFileCache(TileInfo tileInfo, Bitmap bitmap) { using (var ms = new MemoryStream()) { bitmap.Save(ms, _ImageFormat); ms.Seek(0, SeekOrigin.Begin); var data = new byte[ms.Length]; ms.Read(data, 0, data.Length); _fileCache.Add(tileInfo.Index, data); } }
public void FlushRegionTest() { _cache = new FileCache("FlushRegionTest"); _cache.Add("Key1", "Value1", _cache.DefaultPolicy); _cache.Add("Key2", "Value2", _cache.DefaultPolicy, "Region1"); Thread.Sleep(500); //attempt flush _cache.Flush("Region1"); Thread.Sleep(500); object result = _cache["Key1"]; result.Should().Be("Value1"); object result2 = _cache.Get("Key2", "Region1"); result2.Should().BeNull(); }
private static void DownloadTile(object tile) { var parameters = (object[])tile; if (parameters.Length != 2) { throw new ArgumentException("Two parameters expected"); } var tileInfo = (TileInfo)parameters[0]; var doneEvent = (MultipleThreadResetEvent)parameters[1]; if (!_trackCancel.Continue()) { doneEvent.SetOne(); //!!!multipleThreadResetEvent.SetOne(); return; } if (!_fileCache.Exists(tileInfo.Index)) { var url = _tileProvider.Request.GetUri(tileInfo); Logger.Debug("Url: " + url); var bytes = GetBitmap(url); try { if (bytes != null) { var name = _fileCache.GetFileName(tileInfo.Index); _fileCache.Add(tileInfo.Index, bytes); CreateRaster(tileInfo, name); Logger.Debug("Tile retrieved: " + url.AbsoluteUri); } } catch (Exception) { } } _showTiles.Add(tileInfo); if (tileInfo != null) { var name = _fileCache.GetFileName(tileInfo.Index); if (File.Exists(name)) { DrawRaster(name); } } doneEvent.SetOne(); }
public void CustomRegionTest() { var cache = new FileCache(); var policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, 5, 0) }; var key = "my_key"; var region = "my_region"; object value = null; cache.Add(key, "foo", policy, region); cache.Add(key, "bar", policy); value = cache.Get(key); // returns: "bar" Assert.AreEqual("bar", value); value = cache.Get(key, region); // returns: "foo" Assert.AreEqual("foo", value); value = cache.Get(key); // returns: "foo" ?! Assert.AreEqual("bar", value); }
public void CustomRegionTest() { _cache = new FileCache(); var policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, 5, 0) }; var key = "my_key"; var region = "my_region"; object value = null; _cache.Add(key, "foo", policy, region); _cache.Add(key, "bar", policy); value = _cache.Get(key); // returns: "bar" value.Should().Be("bar"); value = _cache.Get(key, region); // returns: "foo" value.Should().Be("foo"); value = _cache.Get(key); // returns: "foo" ?! value.Should().Be("bar"); }
public void GetTile_WhenTilePresent_ShouldReturnTile() { // arrange var tileCache = new FileCache(".", "png", new TimeSpan(long.MaxValue)); tileCache.Add(new TileIndex(4, 5, 8),new byte[243]); var fileTileProvider = new FileTileProvider(".", "png", new TimeSpan(long.MaxValue)); // act var tile = fileTileProvider.GetTile(new TileInfo { Index = new TileIndex(4, 5, 8) }); // assert Assert.AreEqual(tile.Length, 243); }
private void Fetch(TileInfo tileInfo) { try { var data = _tileSource.Provider.GetTile(tileInfo); _fileCache.Add(tileInfo.Index, data); _fileCache.AddWorldFile(tileInfo, _tileSource.Schema.GetTileHeight("0"), _tileSource.Schema.GetTileHeight("0"), _tileSource.Schema.Format); } catch (Exception) { } }
private async Task <T> GetWithDebugCaching <T>(Func <Task <T> > get, string methodName, string debugCacheKey) { debugCacheKey = methodName + debugCacheKey; var cache = new FileCache(); if (cache[debugCacheKey] != null) { return(((string)cache[debugCacheKey]).FromJson <T>()); } var result = await get.Invoke(); cache.Add(debugCacheKey, result.ToJson(), DateTimeOffset.Now.AddDays(1)); return(result); }
public void Create(string key, MemoryStream stream) { var cachedItems = cache.GetCount(); if (cachedItems >= configuration.MaxCachedCount) { throw new Exception("Max items limit is exceeded"); } cache.Add( new CacheItem(key, stream.ToArray()), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.Add(configuration.ExpirationTime) }); }
public void GetTile_WhenTilePresent_ShouldReturnTile() { // arrange var tileCache = new FileCache(".", "png", new TimeSpan(long.MaxValue)); tileCache.Add(new TileIndex(4, 5, "8"), new byte[243]); var fileTileProvider = new FileTileProvider(".", "png", new TimeSpan(long.MaxValue)); // act var tile = fileTileProvider.GetTile(new TileInfo { Index = new TileIndex(4, 5, "8") }); // assert Assert.AreEqual(tile.Length, 243); }
private static void DownloadTile(object tile) { var parameters = (object[])tile; if (parameters.Length != 2) { throw new ArgumentException("Two parameters expected"); } var tileInfo = (TileInfo)parameters[0]; var doneEvent = (MultipleThreadResetEvent)parameters[1]; if (!_trackCancel.Continue()) { doneEvent.SetOne(); return; } var url = _tileProvider.Request.GetUri(tileInfo); if (_auth != null) { var uriBuilder = new UriBuilder(url) { Query = _auth }; url = uriBuilder.Uri; } Logger.Debug("Download tile: " + url); var bytes = GetBitmap(url); if (bytes != null) { // hacky.... if (url.AbsoluteUri.Contains("strava")) { bytes = DoStravaHack(bytes); } var name = _fileCache.GetFileName(tileInfo.Index); _fileCache.Add(tileInfo.Index, bytes); CreateRaster(tileInfo, name); Logger.Debug("Tile retrieved: " + url.AbsoluteUri); } doneEvent.SetOne(); }
public void ThreadPoolCallback(object threadContext) { try { if (!FileCache.Has(file)) { object data = DoLoad(file, ext); if (data != null) { FileCache.Add(file, ext, data); } } } finally { if (Interlocked.Decrement(ref pendingThreads) == 0) { doneEvent.Set(); } } }
public static object Load(string file) { file = file.Trim(); file = file.Replace("\\", "/"); if (batching) { if (!batch.Contains(file)) { batch.Add(file); } return(null); } if (!string.IsNullOrEmpty(file)) { string ext = rext.Match(file).Value.Substring(1).ToLower(); if (!string.IsNullOrEmpty(ext)) { if (FileCache.Has(file)) { return(FileCache.Get(file, ext)); } else { object data = DoLoad(file, ext); if (data != null) { if (FileCache.Add(file, ext, data)) { return(FileCache.Get(file, ext)); } else { return(data); } } } } } return(null); }
public void ClearCache() { _cache = new FileCache(); _cache["MyFirstKey"] = "MyFirstValue"; _cache.Add("MySecondKey", "MySecondValue", _cache.DefaultPolicy, "MyFirstRegion"); object pull = _cache.Get("MyFirstKey"); pull.ToString().Should().Be("MyFirstValue"); object pull2 = _cache.Get("MySecondKey", "MyFirstRegion"); pull2.ToString().Should().Be("MySecondValue"); _cache.Clear(); object result = _cache.Get("MyFirstKey"); result.Should().BeNull(); object result2 = _cache.Get("MySecondKey", "MyFirstRegion"); result2.Should().BeNull(); }
private void FetchAsync(TileInfo tileInfo) { ThreadPool.QueueUserWorkItem( source => { Exception error = null; Tile <T> tile = null; try { if (_tileSource != null) { byte[] data = _tileSource.Provider.GetTile(tileInfo); _fileCache.Add(tileInfo.Index, data); _fileCache.AddWorldFile(tileInfo, _tileSource.Schema.GetTileHeight("0"), _tileSource.Schema.GetTileHeight("0"), _tileSource.Schema.Format); tile = new Tile <T> { Data = data, Info = tileInfo }; } } catch (Exception ex) //This may seem a bit weird. We catch the exception to pass it as an argument. This is because we are on a worker thread here, we cannot just let it fall through. { error = ex; } lock (_tilesInProgress) { if (_tilesInProgress.Contains(tileInfo.Index)) { _tilesInProgress.Remove(tileInfo.Index); } } _waitHandle.Set(); if (DataChanged != null && !_isAborted) { DataChanged(this, new DataChangedEventArgs <T>(error, false, tile)); } }); }
public static ISettings CreateSettings(string fileName, bool watch) { if ((fileName == null) || (fileName == string.Empty)) { throw new ArgumentException("fileName"); } Settings item = null; if (__settings.ContainsKey(fileName)) { return(__settings[fileName] as Settings); } item = new Settings(fileName); item.LoadFrom(fileName); if (watch) { __checker.Add(item); } return(item); }
public void ThreadPoolCallback(object state) { try { object[] parameters = state as object[]; System.Action callback = (System.Action)parameters[1]; if (!FileCache.Has(file)) { object data = DoLoad(file, ext); callback?.Invoke(); if (data != null) { FileCache.Add(file, ext, data); } } } finally { if (Interlocked.Decrement(ref pendingThreads) == 0) { doneEvent.Set(); } } }
public void ShrinkCacheTest() { _cache = new FileCache("ShrinkTest"); Thread.Sleep(500); // Added because appveyor would get -1L in the assertion below. So it wasn't able to acquire the lock // Test empty case _cache.ShrinkCacheToSize(0).Should().Be(0); // Insert 4 items, one of them in a region, and keep track of their size //sleep to make sure that oldest item gets removed first _cache["item1"] = "bar1asdfasdfdfskjslkjlkjsdf sdlfkjasdlf asdlfkjskjfkjs d sdkfjksjd"; Thread.Sleep(500); long size1 = _cache.GetCacheSize(); _cache.Add("item2", "bar2sdfjkjk skdfj sdflkj sdlkj lkjkjkjkjkjssss", _cache.DefaultPolicy, "region"); Thread.Sleep(500); long size2 = _cache.GetCacheSize() - size1; _cache["item3"] = "bar3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; Thread.Sleep(500); long size3 = _cache.GetCacheSize() - size2 - size1; _cache["item4"] = "bar3fffffffffsdfsdfffffffffffffsdfffffffffffdddddddddddddddddddffffff"; long size4 = _cache.GetCacheSize() - size3 - size2 - size1; // Shrink to the size of the last 3 items (should remove item1 because it's the oldest, keeping the other 3) long newSize = _cache.ShrinkCacheToSize(size4 + size3 + size2); newSize.Should().Be(size4 + size3 + size2); // Shrink to just smaller than two items (should keep just item4, delete item2 (in a region) and item3) newSize = _cache.ShrinkCacheToSize(size3 + size4 - 1); newSize.Should().Be(size4); // Shrink to size 1 (should delete everything) newSize = _cache.ShrinkCacheToSize(1); newSize.Should().Be(0); }