New: Cache searches for 5 days

pull/965/head
ta264 4 years ago
parent 996841db45
commit d078dacaab

@ -1,4 +1,4 @@
using System; using System;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
@ -33,7 +33,7 @@ namespace NzbDrone.Core.Test.Framework
var httpClient = Mocker.Resolve<IHttpClient>(); var httpClient = Mocker.Resolve<IHttpClient>();
Mocker.GetMock<ICachedHttpResponseService>() Mocker.GetMock<ICachedHttpResponseService>()
.Setup(x => x.Get(It.IsAny<HttpRequest>(), It.IsAny<bool>(), It.IsAny<TimeSpan>())) .Setup(x => x.Get(It.IsAny<HttpRequest>(), It.IsAny<bool>(), It.IsAny<TimeSpan>()))
.Returns((HttpRequest request, bool useCavhe, TimeSpan ttl) => httpClient.Get(request)); .Returns((HttpRequest request, bool useCache, TimeSpan ttl) => httpClient.Get(request));
} }
} }

@ -3,7 +3,9 @@ using System.Collections.Generic;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Core.Books; using NzbDrone.Core.Books;
using NzbDrone.Core.Http;
using NzbDrone.Core.MetadataSource.Goodreads; using NzbDrone.Core.MetadataSource.Goodreads;
using NzbDrone.Core.Profiles.Metadata; using NzbDrone.Core.Profiles.Metadata;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
@ -19,6 +21,11 @@ namespace NzbDrone.Core.Test.MetadataSource.Goodreads
{ {
UseRealHttp(); UseRealHttp();
var httpClient = Mocker.Resolve<IHttpClient>();
Mocker.GetMock<ICachedHttpResponseService>()
.Setup(x => x.Get<List<SearchJsonResource>>(It.IsAny<HttpRequest>(), It.IsAny<bool>(), It.IsAny<TimeSpan>()))
.Returns((HttpRequest request, bool useCache, TimeSpan ttl) => httpClient.Get<List<SearchJsonResource>>(request));
var metadataProfile = new MetadataProfile(); var metadataProfile = new MetadataProfile();
Mocker.GetMock<IMetadataProfileService>() Mocker.GetMock<IMetadataProfileService>()

@ -7,6 +7,8 @@ namespace NzbDrone.Core.Http
public interface ICachedHttpResponseService public interface ICachedHttpResponseService
{ {
HttpResponse Get(HttpRequest request, bool useCache, TimeSpan ttl); HttpResponse Get(HttpRequest request, bool useCache, TimeSpan ttl);
HttpResponse<T> Get<T>(HttpRequest request, bool useCache, TimeSpan ttl)
where T : new();
} }
public class CachedHttpResponseService : ICachedHttpResponseService public class CachedHttpResponseService : ICachedHttpResponseService
@ -54,5 +56,12 @@ namespace NzbDrone.Core.Http
return result; return result;
} }
public HttpResponse<T> Get<T>(HttpRequest request, bool useCache, TimeSpan ttl)
where T : new()
{
var response = Get(request, useCache, ttl);
return new HttpResponse<T>(response);
}
} }
} }

@ -28,7 +28,6 @@ namespace NzbDrone.Core.MetadataSource.Goodreads
private static readonly Regex NoPhotoRegex = new Regex(@"/nophoto/(book|user)/", private static readonly Regex NoPhotoRegex = new Regex(@"/nophoto/(book|user)/",
RegexOptions.IgnoreCase | RegexOptions.Compiled); RegexOptions.IgnoreCase | RegexOptions.Compiled);
private readonly IHttpClient _httpClient;
private readonly ICachedHttpResponseService _cachedHttpClient; private readonly ICachedHttpResponseService _cachedHttpClient;
private readonly Logger _logger; private readonly Logger _logger;
private readonly IAuthorService _authorService; private readonly IAuthorService _authorService;
@ -38,15 +37,13 @@ namespace NzbDrone.Core.MetadataSource.Goodreads
private readonly IHttpRequestBuilderFactory _searchBuilder; private readonly IHttpRequestBuilderFactory _searchBuilder;
private readonly ICached<HashSet<string>> _cache; private readonly ICached<HashSet<string>> _cache;
public GoodreadsProxy(IHttpClient httpClient, public GoodreadsProxy(ICachedHttpResponseService cachedHttpClient,
ICachedHttpResponseService cachedHttpClient,
IAuthorService authorService, IAuthorService authorService,
IBookService bookService, IBookService bookService,
IEditionService editionService, IEditionService editionService,
Logger logger, Logger logger,
ICacheManager cacheManager) ICacheManager cacheManager)
{ {
_httpClient = httpClient;
_cachedHttpClient = cachedHttpClient; _cachedHttpClient = cachedHttpClient;
_authorService = authorService; _authorService = authorService;
_bookService = bookService; _bookService = bookService;
@ -482,7 +479,7 @@ namespace NzbDrone.Core.MetadataSource.Goodreads
.AddQueryParam("q", query) .AddQueryParam("q", query)
.Build(); .Build();
var result = _httpClient.Get<List<SearchJsonResource>>(httpRequest); var result = _cachedHttpClient.Get<List<SearchJsonResource>>(httpRequest, true, TimeSpan.FromDays(5));
return result.Resource.SelectList(MapJsonSearchResult); return result.Resource.SelectList(MapJsonSearchResult);
} }

Loading…
Cancel
Save