| | | 1 | | using GMap.NET; |
| | | 2 | | using GMap.NET.WindowsPresentation; |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | using System.IO; |
| | | 5 | | |
| | | 6 | | // thanks to GPT5-mini for greate basic help here! |
| | | 7 | | |
| | | 8 | | namespace OffRouteMap |
| | | 9 | | { |
| | | 10 | | |
| | | 11 | | public class FileCacheProvider : PureImageCache, IDisposable |
| | | 12 | | { |
| | | 13 | | readonly string root; |
| | | 14 | | readonly long maxCacheBytes; |
| | 4 | 15 | | readonly ReaderWriterLockSlim rw = new ReaderWriterLockSlim(); |
| | 4 | 16 | | readonly ConcurrentQueue<string> cleanupQueue = new ConcurrentQueue<string>(); |
| | | 17 | | readonly Thread cleanupThread; |
| | | 18 | | bool disposed; |
| | | 19 | | |
| | 4 | 20 | | public FileCacheProvider (string rootPath, long maxCacheBytes = 0) |
| | | 21 | | { |
| | 4 | 22 | | root = rootPath ?? throw new ArgumentNullException(nameof(rootPath)); |
| | 4 | 23 | | this.maxCacheBytes = maxCacheBytes; |
| | 4 | 24 | | Directory.CreateDirectory(root); |
| | | 25 | | |
| | 4 | 26 | | if (maxCacheBytes > 0) |
| | | 27 | | { |
| | 0 | 28 | | cleanupThread = new Thread(BackgroundCleanup) { IsBackground = true }; |
| | 0 | 29 | | cleanupThread.Start(); |
| | | 30 | | } |
| | 4 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Store map tile as file. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="tile">raw image bytes (png/jpg)</param> |
| | | 37 | | /// <param name="type">provider-specific type (can be ignored or used to subfolder)</param> |
| | | 38 | | /// <param name="pos"></param> |
| | | 39 | | /// <param name="zoom"></param> |
| | | 40 | | /// <returns>false on error</returns> |
| | | 41 | | public bool PutImageToCache (byte[] tile, int type, GPoint pos, int zoom) |
| | | 42 | | { |
| | | 43 | | try |
| | | 44 | | { |
| | 0 | 45 | | long x = pos.X; |
| | 0 | 46 | | long y = pos.Y; |
| | | 47 | | |
| | 0 | 48 | | string baseDir = root; |
| | | 49 | | //string folder = (type == 0) ? "" : GMapProviders.TryGetProvider(type).Name; |
| | | 50 | | //var baseDir = string.IsNullOrEmpty(folder) ? root : Path.Combine(root, folder); |
| | 0 | 51 | | Directory.CreateDirectory(baseDir); |
| | | 52 | | |
| | | 53 | | // @todo .png always?! |
| | 0 | 54 | | var path = Path.Combine(baseDir, zoom.ToString(), x.ToString(), y + ".png"); |
| | 0 | 55 | | Directory.CreateDirectory(Path.GetDirectoryName(path)); |
| | | 56 | | |
| | 0 | 57 | | rw.EnterWriteLock(); |
| | | 58 | | try |
| | | 59 | | { |
| | 0 | 60 | | File.WriteAllBytes(path, tile); |
| | 0 | 61 | | File.SetLastWriteTimeUtc(path, DateTime.UtcNow); |
| | 0 | 62 | | } |
| | 0 | 63 | | finally { rw.ExitWriteLock(); } |
| | | 64 | | |
| | 0 | 65 | | if (maxCacheBytes > 0) cleanupQueue.Enqueue(path); |
| | | 66 | | |
| | 0 | 67 | | return true; |
| | | 68 | | } |
| | 0 | 69 | | catch |
| | | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Load map tile from file cache. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="type"></param> |
| | | 79 | | /// <param name="pos"></param> |
| | | 80 | | /// <param name="zoom"></param> |
| | | 81 | | /// <returns>tile as pureImage or null on fail</returns> |
| | | 82 | | public PureImage GetImageFromCache (int type, GPoint pos, int zoom) |
| | | 83 | | { |
| | | 84 | | try |
| | | 85 | | { |
| | 0 | 86 | | long x = pos.X; |
| | 0 | 87 | | long y = pos.Y; |
| | | 88 | | |
| | 0 | 89 | | string baseDir = root; |
| | | 90 | | |
| | | 91 | | //string folder = (type == 0) ? "" : GMapProviders.TryGetProvider(type).Name; |
| | | 92 | | //var baseDir = string.IsNullOrEmpty(folder) ? root : Path.Combine(root, folder); |
| | | 93 | | |
| | | 94 | | // @todo .png always?! |
| | 0 | 95 | | var path = Path.Combine(baseDir, zoom.ToString(), x.ToString(), y + ".png"); |
| | | 96 | | |
| | 0 | 97 | | if (!File.Exists(path)) return null; |
| | | 98 | | |
| | 0 | 99 | | rw.EnterReadLock(); |
| | | 100 | | byte[] bytes; |
| | | 101 | | try |
| | | 102 | | { |
| | 0 | 103 | | bytes = File.ReadAllBytes(path); |
| | 0 | 104 | | File.SetLastWriteTimeUtc(path, DateTime.UtcNow); |
| | 0 | 105 | | } |
| | 0 | 106 | | finally { rw.ExitReadLock(); } |
| | | 107 | | |
| | 0 | 108 | | MemoryStream stm = new MemoryStream(bytes, 0, bytes.Length, false, true); |
| | | 109 | | |
| | 0 | 110 | | var img = GMapImageProxy.Instance.FromStream(stm); |
| | 0 | 111 | | if (img != null) |
| | | 112 | | { |
| | 0 | 113 | | img.Data = stm; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return img; |
| | | 117 | | } |
| | 0 | 118 | | catch |
| | | 119 | | { |
| | 0 | 120 | | return null; |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// delete files older than date; if type != null, limit to that subfolder |
| | | 126 | | /// returns number of deleted tiles |
| | | 127 | | /// </summary> |
| | | 128 | | public int DeleteOlderThan (DateTime date, int? type) |
| | | 129 | | { |
| | 0 | 130 | | int deleted = 0; |
| | | 131 | | try |
| | | 132 | | { |
| | 0 | 133 | | string baseDir = root; |
| | | 134 | | //if (type.HasValue && type.Value != 0) |
| | | 135 | | // baseDir = Path.Combine(root, GMapProviders.TryGetProvider((int)type).Name); |
| | | 136 | | |
| | 0 | 137 | | if (!Directory.Exists(baseDir)) return 0; |
| | | 138 | | |
| | 0 | 139 | | rw.EnterWriteLock(); |
| | | 140 | | try |
| | | 141 | | { |
| | 0 | 142 | | var files = Directory.EnumerateFiles(baseDir, "*.png", SearchOption.AllDirectories) |
| | 0 | 143 | | .Where(f => File.GetLastWriteTimeUtc(f) < date) |
| | 0 | 144 | | .ToList(); |
| | | 145 | | |
| | 0 | 146 | | foreach (var f in files) |
| | | 147 | | { |
| | | 148 | | try |
| | | 149 | | { |
| | 0 | 150 | | File.Delete(f); |
| | 0 | 151 | | deleted++; |
| | 0 | 152 | | } |
| | 0 | 153 | | catch { } |
| | | 154 | | } |
| | 0 | 155 | | } |
| | 0 | 156 | | finally { rw.ExitWriteLock(); } |
| | 0 | 157 | | } |
| | 0 | 158 | | catch { } |
| | 0 | 159 | | return deleted; |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Background cleanup: enforce maxCacheBytes using LRU |
| | | 164 | | /// </summary> |
| | | 165 | | void BackgroundCleanup () |
| | | 166 | | { |
| | 0 | 167 | | while (!disposed) |
| | | 168 | | { |
| | | 169 | | try |
| | | 170 | | { |
| | 0 | 171 | | Thread.Sleep(TimeSpan.FromSeconds(10)); |
| | 0 | 172 | | if (cleanupQueue.IsEmpty && GetCacheSizeBytes() <= maxCacheBytes) continue; |
| | | 173 | | |
| | 0 | 174 | | rw.EnterWriteLock(); |
| | | 175 | | try |
| | | 176 | | { |
| | 0 | 177 | | var files = Directory.EnumerateFiles(root, "*.png", SearchOption.AllDirectories) |
| | 0 | 178 | | .Select(p => new FileInfo(p)) |
| | 0 | 179 | | .OrderBy(fi => fi.LastWriteTimeUtc) |
| | 0 | 180 | | .ToList(); |
| | | 181 | | |
| | 0 | 182 | | long total = files.Sum(f => f.Length); |
| | 0 | 183 | | foreach (var fi in files) |
| | | 184 | | { |
| | 0 | 185 | | if (total <= maxCacheBytes) break; |
| | | 186 | | try |
| | | 187 | | { |
| | 0 | 188 | | total -= fi.Length; |
| | 0 | 189 | | fi.Delete(); |
| | 0 | 190 | | } |
| | 0 | 191 | | catch { } |
| | | 192 | | } |
| | 0 | 193 | | } |
| | 0 | 194 | | finally { rw.ExitWriteLock(); } |
| | 0 | 195 | | } |
| | 0 | 196 | | catch { Thread.Sleep(1000); } |
| | | 197 | | } |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | long GetCacheSizeBytes () |
| | | 201 | | { |
| | | 202 | | try |
| | | 203 | | { |
| | 0 | 204 | | rw.EnterReadLock(); |
| | | 205 | | try |
| | | 206 | | { |
| | 0 | 207 | | if (!Directory.Exists(root)) return 0; |
| | 0 | 208 | | return Directory.EnumerateFiles(root, "*.png", SearchOption.AllDirectories) |
| | 0 | 209 | | .Sum(f => new FileInfo(f).Length); |
| | | 210 | | } |
| | 0 | 211 | | finally { rw.ExitReadLock(); } |
| | | 212 | | } |
| | 0 | 213 | | catch { return 0; } |
| | 0 | 214 | | } |
| | | 215 | | |
| | | 216 | | public void Dispose () |
| | | 217 | | { |
| | 0 | 218 | | disposed = true; |
| | 0 | 219 | | try { cleanupThread?.Join(500); } catch { } |
| | 0 | 220 | | rw?.Dispose(); |
| | 0 | 221 | | } |
| | | 222 | | } |
| | | 223 | | } |