2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using FileDbNs;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
using System.Linq;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
using System.Runtime.Caching;
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
namespace MapControl.Caching
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// ObjectCache implementation based on FileDb, a free and simple No-SQL database by EzTools Software.
|
|
|
|
|
|
/// See http://www.eztools-software.com/tools/filedb/.
|
2012-07-03 18:03:56 +02:00
|
|
|
|
/// </summary>
|
2017-08-04 21:38:58 +02:00
|
|
|
|
public sealed class FileDbCache : ObjectCache, IDisposable
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
|
|
|
|
|
private const string keyField = "Key";
|
|
|
|
|
|
private const string valueField = "Value";
|
|
|
|
|
|
private const string expiresField = "Expires";
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
private readonly FileDb fileDb = new FileDb();
|
|
|
|
|
|
private readonly string dbPath;
|
2012-07-07 17:19:10 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
public FileDbCache(string path, bool autoFlush = true, int autoCleanThreshold = -1)
|
2012-07-07 17:19:10 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
2012-07-07 17:19:10 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
throw new ArgumentException("The parameter path must not be null or empty.");
|
2012-07-07 17:19:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
if (string.IsNullOrEmpty(Path.GetExtension(path)))
|
2012-07-07 17:19:10 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
path = Path.Combine(path, "TileCache.fdb");
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
dbPath = path;
|
2012-07-07 17:19:10 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
fileDb.AutoFlush = autoFlush;
|
|
|
|
|
|
fileDb.AutoCleanThreshold = autoCleanThreshold;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
fileDb.Open(dbPath, false);
|
|
|
|
|
|
|
|
|
|
|
|
Debug.WriteLine("FileDbCache: Opened database with {0} cached items in {1}", fileDb.NumRecords, dbPath);
|
2012-08-13 19:16:59 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
Clean();
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
2012-07-20 21:57:29 +02:00
|
|
|
|
CreateDatabase();
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += (s, e) => Close();
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
get { return string.Empty; }
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override DefaultCacheCapabilities DefaultCacheCapabilities
|
|
|
|
|
|
{
|
2016-07-03 00:03:38 +02:00
|
|
|
|
get { return DefaultCacheCapabilities.AbsoluteExpirations | DefaultCacheCapabilities.SlidingExpirations; }
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object this[string key]
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return Get(key); }
|
|
|
|
|
|
set { Set(key, value, null); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator()
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException("FileDbCache does not support the ability to enumerate items.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<string> keys, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException("FileDbCache does not support the ability to create change monitors.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override long GetCount(string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (regionName != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-07 17:19:10 +02:00
|
|
|
|
if (fileDb.IsOpen)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return fileDb.NumRecords;
|
2012-07-07 17:19:10 +02:00
|
|
|
|
}
|
2012-07-20 21:57:29 +02:00
|
|
|
|
catch (Exception ex)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.NumRecords: " + ex.Message);
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return 0;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-07 17:19:10 +02:00
|
|
|
|
public override bool Contains(string key, string regionName = null)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
if (regionName != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (fileDb.IsOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return fileDb.GetRecordByKey(key, new string[0], false) != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
2012-07-07 17:19:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object Get(string key, string regionName = null)
|
|
|
|
|
|
{
|
2012-07-03 18:03:56 +02:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-07 17:19:10 +02:00
|
|
|
|
if (regionName != null)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (fileDb.IsOpen)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
var record = fileDb.GetRecordByKey(key, new string[] { valueField }, false);
|
|
|
|
|
|
|
|
|
|
|
|
if (record != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return record[0];
|
|
|
|
|
|
}
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
catch (Exception ex)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return null;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override CacheItem GetCacheItem(string key, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var value = Get(key, regionName);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
return value != null ? new CacheItem(key, value) : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IDictionary<string, object> GetValues(IEnumerable<string> keys, string regionName = null)
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return keys.ToDictionary(key => key, key => Get(key, regionName));
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
|
|
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
if (key == null)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (value == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter value must not be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (policy == null)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
throw new ArgumentNullException("The parameter policy must not be null.");
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-07 17:19:10 +02:00
|
|
|
|
if (regionName != null)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-07 17:19:10 +02:00
|
|
|
|
if (fileDb.IsOpen)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
var expiration = DateTime.MaxValue;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (policy.AbsoluteExpiration != InfiniteAbsoluteExpiration)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
expiration = policy.AbsoluteExpiration.DateTime;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
2015-11-28 21:09:25 +01:00
|
|
|
|
else if (policy.SlidingExpiration != NoSlidingExpiration)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
expiration = DateTime.UtcNow + policy.SlidingExpiration;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-03 20:04:10 +01:00
|
|
|
|
if (!AddOrUpdateRecord(key, value, expiration) && RepairDatabase())
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
AddOrUpdateRecord(key, value, expiration);
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-03 00:03:38 +02:00
|
|
|
|
public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2016-07-03 00:03:38 +02:00
|
|
|
|
Set(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-03 00:03:38 +02:00
|
|
|
|
public override void Set(CacheItem item, CacheItemPolicy policy)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2016-07-03 00:03:38 +02:00
|
|
|
|
Set(item.Key, item.Value, policy, item.RegionName);
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var oldValue = Get(key, regionName);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
Set(key, value, policy);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
return oldValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-03 00:03:38 +02:00
|
|
|
|
public override object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return AddOrGetExisting(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
public override CacheItem AddOrGetExisting(CacheItem item, CacheItemPolicy policy)
|
|
|
|
|
|
{
|
|
|
|
|
|
var oldItem = GetCacheItem(item.Key, item.RegionName);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
Set(item, policy);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
return oldItem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object Remove(string key, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var oldValue = Get(key, regionName);
|
|
|
|
|
|
|
|
|
|
|
|
if (oldValue != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
fileDb.DeleteRecordByKey(key);
|
|
|
|
|
|
}
|
2012-07-07 17:19:10 +02:00
|
|
|
|
catch (Exception ex)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.DeleteRecordByKey(\"{0}\"): {1}", key, ex.Message);
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return oldValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-03 18:03:56 +02:00
|
|
|
|
public void Flush()
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (fileDb.IsOpen)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
fileDb.Flush();
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Clean()
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (fileDb.IsOpen)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
int deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThan));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2015-12-03 20:04:10 +01:00
|
|
|
|
if (deleted > 0)
|
2012-08-13 19:16:59 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: Deleted {0} expired items", deleted);
|
2012-08-13 19:16:59 +02:00
|
|
|
|
fileDb.Clean();
|
|
|
|
|
|
}
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
private void Close()
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
if (fileDb.IsOpen)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-07-07 17:19:10 +02:00
|
|
|
|
fileDb.Close();
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-15 21:31:10 +02:00
|
|
|
|
private void CreateDatabase()
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
Close();
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
if (File.Exists(dbPath))
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
File.Delete(dbPath);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dbPath));
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
fileDb.Create(dbPath, new Field[]
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
new Field(keyField, DataTypeEnum.String) { IsPrimaryKey = true },
|
|
|
|
|
|
new Field(valueField, DataTypeEnum.Byte) { IsArray = true },
|
|
|
|
|
|
new Field(expiresField, DataTypeEnum.DateTime)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
Debug.WriteLine("FileDbCache: Created database " + dbPath);
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-15 21:31:10 +02:00
|
|
|
|
private bool RepairDatabase()
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2012-08-15 21:31:10 +02:00
|
|
|
|
try
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2012-08-15 21:31:10 +02:00
|
|
|
|
fileDb.Reindex();
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return true;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
catch (Exception ex)
|
2012-07-20 21:57:29 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.Reindex(): " + ex.Message);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateDatabase();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
Debug.WriteLine("FileDbCache: Failed creating database {0}: {1}", dbPath, ex.Message);
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return false;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-03 20:04:10 +01:00
|
|
|
|
private bool AddOrUpdateRecord(string key, object value, DateTime expiration)
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
var fieldValues = new FieldValues(3);
|
2012-07-03 18:03:56 +02:00
|
|
|
|
fieldValues.Add(valueField, value);
|
2015-12-03 20:04:10 +01:00
|
|
|
|
fieldValues.Add(expiresField, expiration);
|
2012-07-03 18:03:56 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
bool recordExists;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2012-07-03 18:03:56 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
recordExists = fileDb.GetRecordByKey(key, new string[0], false) != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (recordExists)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
fileDb.UpdateRecordByKey(key, fieldValues);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.UpdateRecordByKey(\"{0}\"): {1}", key, ex.Message);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
fieldValues.Add(keyField, key);
|
|
|
|
|
|
fileDb.AddRecord(fieldValues);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("FileDbCache: FileDb.AddRecord(\"{0}\"): {1}", key, ex.Message);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2015-12-03 20:04:10 +01:00
|
|
|
|
//Debug.WriteLine("FileDbCache: Writing \"{0}\", Expires {1}", key, expiration.ToLocalTime());
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return true;
|
2012-07-03 18:03:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|