diff --git a/MapControl/Shared/ImageFileCache.cs b/MapControl/Shared/ImageFileCache.cs index 1db29c82..b09b0bac 100644 --- a/MapControl/Shared/ImageFileCache.cs +++ b/MapControl/Shared/ImageFileCache.cs @@ -37,6 +37,7 @@ namespace MapControl.Caching } rootDirectory = new DirectoryInfo(path); + rootDirectory.Create(); Debug.WriteLine($"{nameof(ImageFileCache)}: {rootDirectory.FullName}"); @@ -133,10 +134,14 @@ namespace MapControl.Caching { if (file != null && buffer?.Length > 0) { - using (var stream = CreateFile(file, options)) + file.Directory.Create(); + + using (var stream = file.Create()) { stream.Write(buffer, 0, buffer.Length); } + + SetExpiration(file, options); } } catch (Exception ex) @@ -155,10 +160,14 @@ namespace MapControl.Caching { if (file != null && buffer?.Length > 0 && !token.IsCancellationRequested) { - using (var stream = CreateFile(file, options)) + file.Directory.Create(); + + using (var stream = file.Create()) { await stream.WriteAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false); } + + SetExpiration(file, options); } } catch (Exception ex) @@ -254,24 +263,11 @@ namespace MapControl.Caching return null; } - private static FileStream CreateFile(FileInfo file, DistributedCacheEntryOptions options) + private static void SetExpiration(FileInfo file, DistributedCacheEntryOptions options) { - file.Directory.Create(); - - var stream = file.Create(); - - try - { - file.CreationTime = options.AbsoluteExpiration.HasValue - ? options.AbsoluteExpiration.Value.LocalDateTime - : DateTime.Now.Add(options.AbsoluteExpirationRelativeToNow ?? (options.SlidingExpiration ?? TimeSpan.FromDays(1))); - } - catch (Exception ex) - { - Debug.WriteLine($"{nameof(ImageFileCache)}: Failed setting creation time of {file.FullName}: {ex.Message}"); - } - - return stream; + file.CreationTime = options.AbsoluteExpiration.HasValue + ? options.AbsoluteExpiration.Value.LocalDateTime + : DateTime.Now.Add(options.AbsoluteExpirationRelativeToNow ?? (options.SlidingExpiration ?? TimeSpan.FromDays(1))); } private static int CleanDirectory(DirectoryInfo directory) diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index db292adc..22f2186c 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -29,8 +29,11 @@ namespace MapControl /// Default folder path where a persistent cache implementation may save data, i.e. "C:\ProgramData\MapControl\TileCache". /// public static string DefaultCacheFolder => +#if UWP + Path.Combine(Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path, "TileCache"); +#else Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache"); - +#endif /// /// An IDistributedCache implementation used to cache tile images. /// The default value is a MemoryDistributedCache instance. diff --git a/MapControl/UWP/MapControl.UWP.csproj b/MapControl/UWP/MapControl.UWP.csproj index 7dd2d1f9..35ee99a3 100644 --- a/MapControl/UWP/MapControl.UWP.csproj +++ b/MapControl/UWP/MapControl.UWP.csproj @@ -86,6 +86,9 @@ GroundOverlay.cs + + ImageFileCache.cs + ImageLoader.cs @@ -275,7 +278,6 @@ Tile.WinUI.cs - diff --git a/SampleApps/UniversalApp/MainPage.xaml.cs b/SampleApps/UniversalApp/MainPage.xaml.cs index 3961564a..2e297f3f 100644 --- a/SampleApps/UniversalApp/MainPage.xaml.cs +++ b/SampleApps/UniversalApp/MainPage.xaml.cs @@ -17,7 +17,7 @@ namespace SampleApplication static MainPage() { //MapProjectionFactory.Instance = new MapControl.Projections.GeoApiProjectionFactory(); - //TileImageLoader.Cache = new MapControl.Caching.ImageFileCache(ApplicationData.Current.LocalCacheFolder); + //TileImageLoader.Cache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder); } public MainPage()