mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Unified ImageFileCache implementations
This commit is contained in:
parent
f9347edf19
commit
642c3066fb
2 changed files with 112 additions and 112 deletions
|
|
@ -5,7 +5,9 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
|
|
@ -30,6 +32,11 @@ namespace MapControl.Caching
|
|||
Debug.WriteLine("Created ImageFileCache in " + rootDirectory);
|
||||
}
|
||||
|
||||
public Task Clean()
|
||||
{
|
||||
return Task.Factory.StartNew(CleanRootDirectory, TaskCreationOptions.LongRunning);
|
||||
}
|
||||
|
||||
private string GetPath(string key)
|
||||
{
|
||||
try
|
||||
|
|
@ -44,6 +51,81 @@ namespace MapControl.Caching
|
|||
return null;
|
||||
}
|
||||
|
||||
private void CleanRootDirectory()
|
||||
{
|
||||
foreach (var dir in new DirectoryInfo(rootDirectory).EnumerateDirectories())
|
||||
{
|
||||
var deletedFileCount = CleanDirectory(dir);
|
||||
|
||||
if (deletedFileCount > 0)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Cleaned {0} files in {1}", deletedFileCount, dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int CleanDirectory(DirectoryInfo directory)
|
||||
{
|
||||
var deletedFileCount = 0;
|
||||
|
||||
foreach (var dir in directory.EnumerateDirectories())
|
||||
{
|
||||
deletedFileCount += CleanDirectory(dir);
|
||||
}
|
||||
|
||||
foreach (var file in directory.EnumerateFiles())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ReadExpiration(file) < DateTime.UtcNow)
|
||||
{
|
||||
file.Delete();
|
||||
deletedFileCount++;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed cleaning {0}: {1}", file.FullName, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
if (!directory.EnumerateFileSystemInfos().Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
directory.Delete();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed cleaning {0}: {1}", directory.FullName, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return deletedFileCount;
|
||||
}
|
||||
|
||||
private static DateTime ReadExpiration(FileInfo file)
|
||||
{
|
||||
DateTime? expiration = null;
|
||||
|
||||
if (file.Length > 16)
|
||||
{
|
||||
var buffer = new byte[16];
|
||||
|
||||
using (var stream = file.OpenRead())
|
||||
{
|
||||
stream.Seek(-16, SeekOrigin.End);
|
||||
|
||||
if (stream.Read(buffer, 0, 16) == 16)
|
||||
{
|
||||
expiration = ReadExpiration(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expiration ?? DateTime.Today;
|
||||
}
|
||||
|
||||
private static DateTime ReadExpiration(ref byte[] buffer)
|
||||
{
|
||||
DateTime? expiration = ReadExpiration(buffer);
|
||||
|
|
@ -59,13 +141,15 @@ namespace MapControl.Caching
|
|||
|
||||
private static DateTime? ReadExpiration(byte[] buffer)
|
||||
{
|
||||
DateTime? expiration = null;
|
||||
|
||||
if (buffer.Length >= 16 &&
|
||||
Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == expiresTag)
|
||||
{
|
||||
return new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
|
||||
expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
return null;
|
||||
return expiration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue