Dropped UWP ImageFileCache

This commit is contained in:
ClemensFischer 2025-02-20 11:45:45 +01:00
parent f99fc777f2
commit 6ea58c9dd3
4 changed files with 23 additions and 22 deletions

View file

@ -37,6 +37,7 @@ namespace MapControl.Caching
} }
rootDirectory = new DirectoryInfo(path); rootDirectory = new DirectoryInfo(path);
rootDirectory.Create();
Debug.WriteLine($"{nameof(ImageFileCache)}: {rootDirectory.FullName}"); Debug.WriteLine($"{nameof(ImageFileCache)}: {rootDirectory.FullName}");
@ -133,10 +134,14 @@ namespace MapControl.Caching
{ {
if (file != null && buffer?.Length > 0) 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); stream.Write(buffer, 0, buffer.Length);
} }
SetExpiration(file, options);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -155,10 +160,14 @@ namespace MapControl.Caching
{ {
if (file != null && buffer?.Length > 0 && !token.IsCancellationRequested) 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); await stream.WriteAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false);
} }
SetExpiration(file, options);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -254,24 +263,11 @@ namespace MapControl.Caching
return null; return null;
} }
private static FileStream CreateFile(FileInfo file, DistributedCacheEntryOptions options) private static void SetExpiration(FileInfo file, DistributedCacheEntryOptions options)
{ {
file.Directory.Create(); file.CreationTime = options.AbsoluteExpiration.HasValue
? options.AbsoluteExpiration.Value.LocalDateTime
var stream = file.Create(); : DateTime.Now.Add(options.AbsoluteExpirationRelativeToNow ?? (options.SlidingExpiration ?? TimeSpan.FromDays(1)));
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;
} }
private static int CleanDirectory(DirectoryInfo directory) private static int CleanDirectory(DirectoryInfo directory)

View file

@ -29,8 +29,11 @@ namespace MapControl
/// Default folder path where a persistent cache implementation may save data, i.e. "C:\ProgramData\MapControl\TileCache". /// Default folder path where a persistent cache implementation may save data, i.e. "C:\ProgramData\MapControl\TileCache".
/// </summary> /// </summary>
public static string DefaultCacheFolder => 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"); Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
#endif
/// <summary> /// <summary>
/// An IDistributedCache implementation used to cache tile images. /// An IDistributedCache implementation used to cache tile images.
/// The default value is a MemoryDistributedCache instance. /// The default value is a MemoryDistributedCache instance.

View file

@ -86,6 +86,9 @@
<Compile Include="..\Shared\GroundOverlay.cs"> <Compile Include="..\Shared\GroundOverlay.cs">
<Link>GroundOverlay.cs</Link> <Link>GroundOverlay.cs</Link>
</Compile> </Compile>
<Compile Include="..\Shared\ImageFileCache.cs">
<Link>ImageFileCache.cs</Link>
</Compile>
<Compile Include="..\Shared\ImageLoader.cs"> <Compile Include="..\Shared\ImageLoader.cs">
<Link>ImageLoader.cs</Link> <Link>ImageLoader.cs</Link>
</Compile> </Compile>
@ -275,7 +278,6 @@
<Compile Include="..\WinUI\Tile.WinUI.cs"> <Compile Include="..\WinUI\Tile.WinUI.cs">
<Link>Tile.WinUI.cs</Link> <Link>Tile.WinUI.cs</Link>
</Compile> </Compile>
<Compile Include="ImageFileCache.UWP.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TileImageLoader.UWP.cs" /> <Compile Include="TileImageLoader.UWP.cs" />
<EmbeddedResource Include="Properties\MapControl.UWP.rd.xml" /> <EmbeddedResource Include="Properties\MapControl.UWP.rd.xml" />

View file

@ -17,7 +17,7 @@ namespace SampleApplication
static MainPage() static MainPage()
{ {
//MapProjectionFactory.Instance = new MapControl.Projections.GeoApiProjectionFactory(); //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() public MainPage()