Replaced ImageCacheItem by Tuple

This commit is contained in:
Clemens 2021-07-02 15:57:01 +02:00
parent 2709f90cdc
commit 77c2169999
14 changed files with 60 additions and 122 deletions

View file

@ -19,7 +19,7 @@ namespace MapControl.Caching
private const string valueField = "Value";
private const string expiresField = "Expires";
private readonly FileDb fileDb = new FileDb() { AutoFlush = true };
private readonly FileDb fileDb = new FileDb { AutoFlush = true };
public FileDbCache(string path)
{
@ -91,7 +91,7 @@ namespace MapControl.Caching
}
catch (Exception ex)
{
Debug.WriteLine("FileDbCache.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("FileDbCache.GetRecordByKey({0}): {1}", key, ex.Message);
}
return null;
@ -114,12 +114,10 @@ namespace MapControl.Caching
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;
Debug.WriteLine("FileDbCache.AddOrUpdateRecord({0}): {1}", key, ex.Message); return;
}
}
}

View file

@ -9,7 +9,7 @@ namespace MapControl.Caching
{
public partial class FileDbCache : IImageCache
{
public Task<ImageCacheItem> GetAsync(string key)
public Task<Tuple<byte[], DateTime>> GetAsync(string key)
{
return Task.Run(() =>
{
@ -20,17 +20,13 @@ namespace MapControl.Caching
return null;
}
return new ImageCacheItem
{
Buffer = (byte[])record[0],
Expiration = (DateTime)record[1]
};
return Tuple.Create((byte[])record[0], (DateTime)record[1]);
});
}
public Task SetAsync(string key, ImageCacheItem cacheItem)
public Task SetAsync(string key, byte[] buffer, DateTime expiration)
{
return Task.Run(() => AddOrUpdateRecord(key, cacheItem.Buffer, cacheItem.Expiration));
return Task.Run(() => AddOrUpdateRecord(key, buffer, expiration));
}
}
}

View file

@ -75,7 +75,7 @@ namespace MapControl.Caching
}
catch (Exception ex)
{
Debug.WriteLine("FileDbCache.Contains(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("FileDbCache.Contains({0}): {1}", key, ex.Message);
}
return false;
@ -100,11 +100,7 @@ namespace MapControl.Caching
return null;
}
return new ImageCacheItem
{
Buffer = (byte[])record[0],
Expiration = (DateTime)record[1]
};
return Tuple.Create((byte[])record[0], (DateTime)record[1]);
}
public override CacheItem GetCacheItem(string key, string regionName = null)
@ -131,12 +127,12 @@ namespace MapControl.Caching
throw new ArgumentNullException(nameof(key));
}
if (!(value is ImageCacheItem imageCacheItem))
if (!(value is Tuple<byte[], DateTime> cacheItem))
{
throw new ArgumentException("The value argument must be a MapControl.Caching.ImageCacheItem instance.", nameof(value));
throw new ArgumentException("The value argument must be a Tuple<byte[], DateTime>.", nameof(value));
}
AddOrUpdateRecord(key, imageCacheItem.Buffer, imageCacheItem.Expiration);
AddOrUpdateRecord(key, cacheItem.Item1, cacheItem.Item2);
}
public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
@ -184,7 +180,7 @@ namespace MapControl.Caching
}
catch (Exception ex)
{
Debug.WriteLine("FileDbCache.Remove(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("FileDbCache.Remove({0}): {1}", key, ex.Message);
}
}

View file

@ -46,8 +46,4 @@
<Reference Include="System.Runtime.Caching" />
<PackageReference Include="FileDb.NET" Version="7.4.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\MapControl\WPF\MapControl.WPF.csproj" />
</ItemGroup>
</Project>