Version 4.13.1 Cache "no tile" responses.

This commit is contained in:
ClemensF 2019-07-22 19:47:57 +02:00
parent 576dd8e8e7
commit 219171381f
27 changed files with 200 additions and 338 deletions

View file

@ -20,7 +20,6 @@ namespace MapControl.Caching
private const string expiresField = "Expires";
private readonly FileDb fileDb = new FileDb() { AutoFlush = true };
private readonly string dbPath;
public void Dispose()
{
@ -29,112 +28,83 @@ namespace MapControl.Caching
public void Clean()
{
if (fileDb.IsOpen)
{
int deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThan));
var deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThan));
if (deleted > 0)
{
Debug.WriteLine("FileDbCache: Deleted {0} expired items", deleted);
fileDb.Clean();
}
if (deleted > 0)
{
Debug.WriteLine("FileDbCache: Deleted {0} expired items.", deleted);
fileDb.Clean();
}
}
private void Open()
private void Open(string path)
{
if (!fileDb.IsOpen)
try
{
try
fileDb.Open(path);
Debug.WriteLine("FileDbCache: Opened database " + path);
Clean();
}
catch
{
if (File.Exists(path))
{
fileDb.Open(dbPath);
Debug.WriteLine("FileDbCache: Opened database " + dbPath);
Clean();
File.Delete(path);
}
catch
else
{
CreateDatabase();
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
fileDb.Create(path, new Field[]
{
new Field(keyField, DataTypeEnum.String) { IsPrimaryKey = true },
new Field(valueField, DataTypeEnum.Byte) { IsArray = true },
new Field(expiresField, DataTypeEnum.DateTime)
});
Debug.WriteLine("FileDbCache: Created database " + path);
}
}
private void Close()
{
if (fileDb.IsOpen)
{
fileDb.Close();
}
}
private void CreateDatabase()
{
Close();
if (File.Exists(dbPath))
{
File.Delete(dbPath);
}
else
{
Directory.CreateDirectory(Path.GetDirectoryName(dbPath));
}
fileDb.Create(dbPath, new Field[]
{
new Field(keyField, DataTypeEnum.String) { IsPrimaryKey = true },
new Field(valueField, DataTypeEnum.Byte) { IsArray = true },
new Field(expiresField, DataTypeEnum.DateTime)
});
Debug.WriteLine("FileDbCache: Created database " + dbPath);
}
private Record GetRecordByKey(string key)
{
Record record = null;
if (fileDb.IsOpen)
try
{
try
{
record = fileDb.GetRecordByKey(key, new string[] { valueField, expiresField }, false);
}
catch (Exception ex)
{
Debug.WriteLine("FileDbCache.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
}
return fileDb.GetRecordByKey(key, new string[] { valueField, expiresField }, false);
}
catch (Exception ex)
{
Debug.WriteLine("FileDbCache.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
}
return record;
return null;
}
private void AddOrUpdateRecord(string key, byte[] value, DateTime expiration)
private void AddOrUpdateRecord(string key, byte[] buffer, DateTime expiration)
{
if (fileDb.IsOpen)
var fieldValues = new FieldValues(3);
fieldValues.Add(valueField, buffer ?? new byte[0]);
fieldValues.Add(expiresField, expiration);
try
{
var fieldValues = new FieldValues(3);
fieldValues.Add(valueField, value);
fieldValues.Add(expiresField, expiration);
try
if (fileDb.GetRecordByKey(key, new string[0], false) != null)
{
if (fileDb.GetRecordByKey(key, new string[0], false) != null)
{
fileDb.UpdateRecordByKey(key, fieldValues);
}
else
{
fieldValues.Add(keyField, key);
fileDb.AddRecord(fieldValues);
}
//Debug.WriteLine("FileDbCache: Writing \"{0}\", Expires {1}", key, imageCacheItem.Expiration.ToLocalTime());
fileDb.UpdateRecordByKey(key, fieldValues);
}
catch (Exception ex)
else
{
Debug.WriteLine("FileDbCache.AddOrUpdateRecord(\"{0}\"): {1}", key, ex.Message); return;
fieldValues.Add(keyField, key);
fileDb.AddRecord(fieldValues);
}
//Debug.WriteLine("FileDbCache: Writing \"{0}\", Expires {1}", key, imageCacheItem.Expiration.ToLocalTime());
}
catch (Exception ex)
{
Debug.WriteLine("FileDbCache.AddOrUpdateRecord(\"{0}\"): {1}", key, ex.Message); return;
}
}
}

View file

@ -8,7 +8,6 @@ using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
namespace MapControl.Caching
{
@ -23,24 +22,14 @@ namespace MapControl.Caching
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentNullException("The parameter fileName must not be null.");
throw new ArgumentException("The parameter fileName must not be null or empty.");
}
dbPath = Path.Combine(folder.Path, "TileCache.fdb");
Open();
Application.Current.Resuming += (s, e) => Open();
Application.Current.Suspending += (s, e) => Close();
Open(Path.Combine(folder.Path, fileName));
}
public Task<ImageCacheItem> GetAsync(string key)
{
if (key == null)
{
throw new ArgumentNullException("The parameter key must not be null.");
}
return Task.Run(() =>
{
var record = GetRecordByKey(key);
@ -60,17 +49,7 @@ namespace MapControl.Caching
public Task SetAsync(string key, IBuffer buffer, DateTime expiration)
{
if (key == null)
{
throw new ArgumentNullException("The parameter key must not be null.");
}
if (buffer == null)
{
throw new ArgumentNullException("The parameter buffer must not be null.");
}
return Task.Run(() => AddOrUpdateRecord(key, buffer.ToArray(), expiration));
return Task.Run(() => AddOrUpdateRecord(key, buffer?.ToArray(), expiration));
}
}
}

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("Copyright © 2019 Clemens Fischer")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.13.0")]
[assembly: AssemblyFileVersion("4.13.0")]
[assembly: AssemblyVersion("4.13.1")]
[assembly: AssemblyFileVersion("4.13.1")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]

View file

@ -25,11 +25,7 @@ namespace MapControl.Caching
path = Path.Combine(path, "TileCache.fdb");
}
dbPath = path;
Open();
AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
Open(path);
}
public override string Name
@ -153,9 +149,9 @@ namespace MapControl.Caching
var imageCacheItem = value as ImageCacheItem;
if (imageCacheItem == null || imageCacheItem.Buffer == null || imageCacheItem.Buffer.Length == 0)
if (imageCacheItem == null)
{
throw new ArgumentException("The parameter value must be an ImageCacheItem with a non-empty Buffer.");
throw new ArgumentException("The parameter value must be a MapControl.Caching.ImageCacheItem instance.");
}
AddOrUpdateRecord(key, imageCacheItem.Buffer, imageCacheItem.Expiration);

View file

@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("Copyright © 2019 Clemens Fischer")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.13.0")]
[assembly: AssemblyFileVersion("4.13.0")]
[assembly: AssemblyVersion("4.13.1")]
[assembly: AssemblyFileVersion("4.13.1")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]