Skip to content

Commit

Permalink
Update IRequestCache interface.
Browse files Browse the repository at this point in the history
Adds a 'Contains' method to test whether a cache entry exists.
  • Loading branch information
wo80 committed Mar 24, 2024
1 parent dd9d75e commit 6fc3c0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/Hqub.MusicBrainz.Client/FileRequestCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public FileRequestCache(string path)
this.Timeout = TimeSpan.FromHours(24.0);
}

/// <inheritdoc />
public async Task Add(string request, Stream response)
{
if (!Directory.Exists(path))
Expand All @@ -48,6 +49,23 @@ public async Task Add(string request, Stream response)
await CacheEntry.Write(path, request, response);
}

/// <inheritdoc />
public Task<bool> Contains(string request)
{
return TryGetCachedItem(request, out Stream stream).ContinueWith(task =>
{
bool exists = task.Result;
if (task.IsCompleted && exists)
{
stream.Dispose();
}
return exists;
});
}

/// <inheritdoc />
public Task<bool> TryGetCachedItem(string request, out Stream stream)
{
var item = CacheEntry.Read(path, request);
Expand All @@ -61,7 +79,7 @@ public Task<bool> TryGetCachedItem(string request, out Stream stream)

if ((DateTime.Now - item.TimeStamp) > Timeout)
{
item.Stream.Close();
item.Stream.Dispose();

return Task.FromResult(false);
}
Expand Down
9 changes: 8 additions & 1 deletion src/Hqub.MusicBrainz/Cache/IRequestCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ public interface IRequestCache
/// Add a request and its response to the cache.
/// </summary>
/// <param name="request">The request url used to identify the cache item.</param>
/// <param name="response">The MusicbBrainz webservice response stream.</param>
/// <param name="response">The MusicbBrainz web service response stream.</param>
Task Add(string request, Stream response);

/// <summary>
/// Test whether the cache contains an entry matching the given request url.
/// </summary>
/// <param name="request">The request url used to identify the cache item.</param>
/// <returns>True, if a cache entry matching the request was found.</returns>
Task<bool> Contains(string request);

/// <summary>
/// Try to find the cached response for given request url.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Hqub.MusicBrainz/Cache/NullCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public Task Add(string request, Stream response)
#endif
}

/// <inheritdoc />
public Task<bool> Contains(string request)
{
return Task.FromResult(false);
}

/// <inheritdoc />
public Task<bool> TryGetCachedItem(string request, out Stream stream)
{
Expand Down

0 comments on commit 6fc3c0e

Please sign in to comment.