diff --git a/src/Hqub.MusicBrainz.Client/FileRequestCache.cs b/src/Hqub.MusicBrainz.Client/FileRequestCache.cs index 1cc1676..b717466 100644 --- a/src/Hqub.MusicBrainz.Client/FileRequestCache.cs +++ b/src/Hqub.MusicBrainz.Client/FileRequestCache.cs @@ -38,6 +38,7 @@ public FileRequestCache(string path) this.Timeout = TimeSpan.FromHours(24.0); } + /// public async Task Add(string request, Stream response) { if (!Directory.Exists(path)) @@ -48,6 +49,23 @@ public async Task Add(string request, Stream response) await CacheEntry.Write(path, request, response); } + /// + public Task Contains(string request) + { + return TryGetCachedItem(request, out Stream stream).ContinueWith(task => + { + bool exists = task.Result; + + if (task.IsCompleted && exists) + { + stream.Dispose(); + } + + return exists; + }); + } + + /// public Task TryGetCachedItem(string request, out Stream stream) { var item = CacheEntry.Read(path, request); @@ -61,7 +79,7 @@ public Task TryGetCachedItem(string request, out Stream stream) if ((DateTime.Now - item.TimeStamp) > Timeout) { - item.Stream.Close(); + item.Stream.Dispose(); return Task.FromResult(false); } diff --git a/src/Hqub.MusicBrainz/Cache/IRequestCache.cs b/src/Hqub.MusicBrainz/Cache/IRequestCache.cs index f64dbd1..7bafe79 100644 --- a/src/Hqub.MusicBrainz/Cache/IRequestCache.cs +++ b/src/Hqub.MusicBrainz/Cache/IRequestCache.cs @@ -13,9 +13,16 @@ public interface IRequestCache /// Add a request and its response to the cache. /// /// The request url used to identify the cache item. - /// The MusicbBrainz webservice response stream. + /// The MusicbBrainz web service response stream. Task Add(string request, Stream response); + /// + /// Test whether the cache contains an entry matching the given request url. + /// + /// The request url used to identify the cache item. + /// True, if a cache entry matching the request was found. + Task Contains(string request); + /// /// Try to find the cached response for given request url. /// diff --git a/src/Hqub.MusicBrainz/Cache/NullCache.cs b/src/Hqub.MusicBrainz/Cache/NullCache.cs index 64704a2..f74e52c 100644 --- a/src/Hqub.MusicBrainz/Cache/NullCache.cs +++ b/src/Hqub.MusicBrainz/Cache/NullCache.cs @@ -28,6 +28,12 @@ public Task Add(string request, Stream response) #endif } + /// + public Task Contains(string request) + { + return Task.FromResult(false); + } + /// public Task TryGetCachedItem(string request, out Stream stream) {