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

@ -10,7 +10,7 @@ namespace MapControl.Caching
{
public partial class SQLiteCache : IImageCache
{
public async Task<ImageCacheItem> GetAsync(string key)
public async Task<Tuple<byte[], DateTime>> GetAsync(string key)
{
try
{
@ -20,36 +20,30 @@ namespace MapControl.Caching
if (await reader.ReadAsync())
{
return new ImageCacheItem
{
Expiration = new DateTime((long)reader["expiration"]),
Buffer = (byte[])reader["buffer"]
};
return Tuple.Create((byte[])reader["buffer"], new DateTime((long)reader["expiration"]));
}
}
}
catch (Exception ex)
{
Debug.WriteLine("SQLiteCache.GetAsync(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.GetAsync({0}): {1}", key, ex.Message);
}
return null;
}
public async Task SetAsync(string key, ImageCacheItem cacheItem)
public async Task SetAsync(string key, byte[] buffer, DateTime expiration)
{
try
{
using (var command = SetItemCommand(key, cacheItem.Expiration, cacheItem.Buffer))
using (var command = SetItemCommand(key, expiration, buffer))
{
await command.ExecuteNonQueryAsync();
}
//Debug.WriteLine("SQLiteCache.SetAsync(\"{0}\"): expires {1}", key, cacheItem.Expiration.ToLocalTime());
}
catch (Exception ex)
{
Debug.WriteLine("SQLiteCache.SetAsync(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.SetAsync({0}): {1}", key, ex.Message);
}
}
}

View file

@ -82,7 +82,7 @@ namespace MapControl.Caching
}
catch (Exception ex)
{
Debug.WriteLine("SQLiteCache.Contains(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.Contains({0}): {1}", key, ex.Message);
}
return false;
@ -108,17 +108,13 @@ namespace MapControl.Caching
if (reader.Read())
{
return new ImageCacheItem
{
Expiration = new DateTime((long)reader["expiration"]),
Buffer = (byte[])reader["buffer"]
};
return Tuple.Create((byte[])reader["buffer"], new DateTime((long)reader["expiration"]));
}
}
}
catch (Exception ex)
{
Debug.WriteLine("SQLiteCache.Get(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.Get({0}): {1}", key, ex.Message);
}
return null;
@ -148,23 +144,21 @@ 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));
}
try
{
using (var command = SetItemCommand(key, imageCacheItem.Expiration, imageCacheItem.Buffer))
using (var command = SetItemCommand(key, cacheItem.Item2, cacheItem.Item1))
{
command.ExecuteNonQuery();
}
//Debug.WriteLine("SQLiteCache.Set(\"{0}\"): expires {1}", key, expiration.ToLocalTime());
}
catch (Exception ex)
{
Debug.WriteLine("SQLiteCache.Set(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.Set({0}): {1}", key, ex.Message);
}
}
@ -216,7 +210,7 @@ namespace MapControl.Caching
}
catch (Exception ex)
{
Debug.WriteLine("SQLiteCache.Remove(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.Remove({0}): {1}", key, ex.Message);
}
}

View file

@ -48,8 +48,4 @@
<ItemGroup>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.114.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\MapControl\WPF\MapControl.WPF.csproj" />
</ItemGroup>
</Project>