Skip to content

Commit b706fa9

Browse files
authored
Fix for throws KeyNotFoundException while Get(key) (#100)
* Fix for throws KeyNotFoundException while Get(key) Fix for #51 Add test to show operates correctly * Fix warnings and merge from main
1 parent 5962eec commit b706fa9

File tree

6 files changed

+31
-44
lines changed

6 files changed

+31
-44
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<GenerateDocumentationFile>true</GenerateDocumentationFile>
4-
<NoWarn>$(NoWarn);1591;1701;1702;1705;VSX1000</NoWarn>
4+
<NoWarn>$(NoWarn);1591;1701;1702;1705;VSX1000;IDE0190</NoWarn>
55
<Platform>AnyCPU</Platform>
66
<IsTestProject>$(MSBuildProjectName.Contains('Tests'))</IsTestProject>
77
<DebugType>embedded</DebugType>

src/ReactiveMarbles.CacheDatabase.EncryptedSettings.Tests/SettingsCacheTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class SettingsCacheTests
1414
/// <summary>
1515
/// Test1s this instance.
1616
/// </summary>
17-
/// <param name="overrideDatabaseName">Name of the override database.</param>
1817
[Fact]
1918
public async void TestCreateAndInsert()
2019
{

src/ReactiveMarbles.CacheDatabase.Settings/Core/AppInfo.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ static AppInfo()
8585
/// Overrides the settings cache path.
8686
/// </summary>
8787
/// <param name="path">The path.</param>
88-
public static void OverrideSettingsCachePath(string path)
89-
{
90-
SettingsCachePath = path;
91-
}
88+
public static void OverrideSettingsCachePath(string path) => SettingsCachePath = path;
9289

9390
/// <summary>
9491
/// Deletes the settings store.

src/ReactiveMarbles.CacheDatabase.Settings/Core/SettingsStorage.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ public void Dispose()
8383
/// <returns>
8484
/// A task that represents the asynchronous dispose operation.
8585
/// </returns>
86-
public ValueTask DisposeAsync() => _blobCache.DisposeAsync();
86+
public ValueTask DisposeAsync()
87+
{
88+
var result = _blobCache.DisposeAsync();
89+
GC.SuppressFinalize(this);
90+
return result;
91+
}
8792

8893
/// <summary>
8994
/// Gets the value for the specified key, or, if the value doesn't exist, saves the <paramref name="defaultValue" /> and returns it.

src/ReactiveMarbles.CacheDatabase.Sqlite3/SqliteBlobCache.cs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -171,43 +171,10 @@ public IObservable<Unit> Flush(Type type)
171171
}
172172

173173
var time = DateTimeOffset.UtcNow;
174-
return _initialized
175-
.SelectMany(async _ =>
176-
{
177-
try
178-
{
179-
var value = await Connection.GetAsync<CacheEntry>(key).ConfigureAwait(false);
180-
181-
if (value is null)
182-
{
183-
throw new KeyNotFoundException($"Key {key} could not be found.");
184-
}
185-
186-
if (value.TypeName is not null)
187-
{
188-
throw new KeyNotFoundException($"Key {key} is associated with an object.");
189-
}
190-
191-
if (value.Id is null)
192-
{
193-
throw new KeyNotFoundException($"Key {key} is id is null.");
194-
}
195-
196-
if (value.ExpiresAt <= time)
197-
{
198-
throw new KeyNotFoundException($"Key {key} has expired.");
199-
}
200-
201-
return value;
202-
}
203-
catch (Exception)
204-
{
205-
throw new KeyNotFoundException($"Key {key} could not be found.");
206-
}
207-
})
208-
.Select(x => x.Value)
209-
.Where(x => x is not null)
210-
.Select(x => x!);
174+
return _initialized.SelectMany((_, _, _) => Connection.Table<CacheEntry>().FirstAsync(x => x.Id != null && x.ExpiresAt > time && x.Id == key))
175+
.Catch<CacheEntry, InvalidOperationException>(ex => Observable.Throw<CacheEntry>(new KeyNotFoundException(ex.Message)))
176+
.Where(x => x?.Value is not null)
177+
.Select(x => x.Value!);
211178
}
212179

213180
/// <inheritdoc/>

src/ReactiveMarbles.CacheDatabase.Tests/BlobCacheTestsBase.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,25 @@ public async Task VacuumPurgeEntriesThatAreExpired()
11851185
}
11861186
}
11871187

1188+
/// <summary>
1189+
/// Tests the issue.
1190+
/// </summary>
1191+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1192+
[Fact]
1193+
public async Task TestIssueAsync()
1194+
{
1195+
using (Utility.WithEmptyDirectory(out var path))
1196+
await using (var fixture = CreateBlobCache(path))
1197+
{
1198+
var cacheKey = "cacheKey";
1199+
var someObject = new string[] { "someObject" };
1200+
await fixture.InsertObject(cacheKey, someObject.AsEnumerable(), null);
1201+
Assert.NotNull(await fixture.GetObject<IEnumerable<string>>(cacheKey));
1202+
Assert.NotNull(await fixture.Get(cacheKey, typeof(IEnumerable<string>)));
1203+
Assert.NotNull(await fixture.Get(cacheKey));
1204+
}
1205+
}
1206+
11881207
/// <summary>
11891208
/// Gets the <see cref="IBlobCache"/> we want to do the tests against.
11901209
/// </summary>

0 commit comments

Comments
 (0)