XAML-Map-Control/FileDbCache/Shared/FileDbCache.cs

112 lines
3.4 KiB
C#
Raw Normal View History

2019-07-21 00:17:16 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// <20> 2019 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using FileDbNs;
using System;
using System.Diagnostics;
using System.IO;
namespace MapControl.Caching
{
/// <summary>
/// Image cache implementation based on FileDb, a free and simple No-SQL database by EzTools Software.
/// See http://www.eztools-software.com/tools/filedb/.
/// </summary>
public sealed partial class FileDbCache : IDisposable
{
private const string keyField = "Key";
private const string valueField = "Value";
private const string expiresField = "Expires";
private readonly FileDb fileDb = new FileDb() { AutoFlush = true };
public void Dispose()
{
fileDb.Dispose();
}
public void Clean()
{
var deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThan));
2019-07-21 00:17:16 +02:00
if (deleted > 0)
{
Debug.WriteLine("FileDbCache: Deleted {0} expired items.", deleted);
fileDb.Clean();
2019-07-21 00:17:16 +02:00
}
}
private void Open(string path)
2019-07-21 00:17:16 +02:00
{
try
2019-07-21 00:17:16 +02:00
{
fileDb.Open(path);
Debug.WriteLine("FileDbCache: Opened database " + path);
2019-07-21 00:17:16 +02:00
Clean();
}
catch
{
if (File.Exists(path))
{
File.Delete(path);
2019-07-21 00:17:16 +02:00
}
else
2019-07-21 00:17:16 +02:00
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
2019-07-21 00:17:16 +02:00
}
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);
2019-07-21 00:17:16 +02:00
}
}
private Record GetRecordByKey(string key)
2019-07-21 00:17:16 +02:00
{
try
2019-07-21 00:17:16 +02:00
{
return fileDb.GetRecordByKey(key, new string[] { valueField, expiresField }, false);
2019-07-21 00:17:16 +02:00
}
catch (Exception ex)
2019-07-21 00:17:16 +02:00
{
Debug.WriteLine("FileDbCache.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
2019-07-21 00:17:16 +02:00
}
return null;
2019-07-21 00:17:16 +02:00
}
private void AddOrUpdateRecord(string key, byte[] buffer, DateTime expiration)
2019-07-21 00:17:16 +02:00
{
var fieldValues = new FieldValues(3);
fieldValues.Add(valueField, buffer ?? new byte[0]);
fieldValues.Add(expiresField, expiration);
2019-07-21 00:17:16 +02:00
try
2019-07-21 00:17:16 +02:00
{
if (fileDb.GetRecordByKey(key, new string[0], false) != null)
2019-07-21 00:17:16 +02:00
{
fileDb.UpdateRecordByKey(key, fieldValues);
2019-07-21 00:17:16 +02:00
}
else
2019-07-21 00:17:16 +02:00
{
fieldValues.Add(keyField, key);
fileDb.AddRecord(fieldValues);
2019-07-21 00:17:16 +02:00
}
//Debug.WriteLine("FileDbCache: Writing \"{0}\", Expires {1}", key, imageCacheItem.Expiration.ToLocalTime());
}
catch (Exception ex)
2019-07-21 00:17:16 +02:00
{
Debug.WriteLine("FileDbCache.AddOrUpdateRecord(\"{0}\"): {1}", key, ex.Message); return;
2019-07-21 00:17:16 +02:00
}
}
}
}