use of org.nzbhydra.mapping.newznab.NewznabParameters in project nzbhydra2 by theotherp.
the class ExternalApi method handleCachingSearch.
protected ResponseEntity<?> handleCachingSearch(NewznabParameters params) {
// Remove old entries
cache.entrySet().removeIf(x -> x.getValue().getLastUpdate().isBefore(clock.instant().minus(MAX_CACHE_AGE_HOURS, ChronoUnit.HOURS)));
CacheEntryValue cacheEntryValue;
if (cache.containsKey(params.cacheKey())) {
cacheEntryValue = cache.get(params.cacheKey());
if (cacheEntryValue.getLastUpdate().isAfter(clock.instant().minus(params.getCachetime(), ChronoUnit.MINUTES))) {
Instant nextUpdate = cacheEntryValue.getLastUpdate().plus(params.getCachetime(), ChronoUnit.MINUTES);
logger.info("Returning cached search result. Next update of search will be done at {}", nextUpdate);
return new ResponseEntity<>(cacheEntryValue.getSearchResult(), HttpStatus.OK);
} else {
logger.info("Updating search because cache time is exceeded");
}
}
// Remove oldest entry when max size is reached
if (cache.size() == MAX_CACHE_SIZE) {
Optional<Entry<Integer, CacheEntryValue>> keyToEvict = cache.entrySet().stream().min(Comparator.comparing(o -> o.getValue().getLastUpdate()));
// Should always be the case anyway
logger.info("Removing oldest entry from cache because its limit of {} is reached", MAX_CACHE_SIZE);
keyToEvict.ifPresent(newznabParametersCacheEntryValueEntry -> cache.remove(newznabParametersCacheEntryValueEntry.getKey()));
}
NewznabResponse searchResult = search(params);
logger.info("Putting search result into cache");
cache.put(params.cacheKey(), new CacheEntryValue(params, clock.instant(), searchResult));
return new ResponseEntity<>(searchResult, HttpStatus.OK);
}
use of org.nzbhydra.mapping.newznab.NewznabParameters in project nzbhydra2 by theotherp.
the class ExternalApiTest method getNewznabParameters.
protected NewznabParameters getNewznabParameters(String q1) {
NewznabParameters parameters = new NewznabParameters();
parameters.setQ(q1);
parameters.setApikey("apikey");
parameters.setT(ActionAttribute.SEARCH);
parameters.setCachetime(5);
return parameters;
}
use of org.nzbhydra.mapping.newznab.NewznabParameters in project nzbhydra2 by theotherp.
the class ExternalApiTest method shouldRepeatSearchWhenCacheTimeIsOver.
@Test
public void shouldRepeatSearchWhenCacheTimeIsOver() throws Exception {
NewznabParameters parameters = new NewznabParameters();
parameters.setQ("q");
parameters.setApikey("apikey");
parameters.setT(ActionAttribute.SEARCH);
parameters.setCachetime(5);
testee.api(parameters);
verify(searcher).search(any());
testee.api(parameters);
verify(searcher, times(1)).search(any());
testee.clock = Clock.fixed(testee.clock.instant().plus(6, ChronoUnit.MINUTES), ZoneId.of("UTC"));
testee.api(parameters);
verify(searcher, times(2)).search(any());
}
use of org.nzbhydra.mapping.newznab.NewznabParameters in project nzbhydra2 by theotherp.
the class ExternalApiSearchingIntegrationTest method shouldCallNewznabTwice.
@Test
public void shouldCallNewznabTwice() throws Exception {
NewznabResponseBuilder builder = new NewznabResponseBuilder();
String xml1 = builder.getTestResult(1, 100, "indexer1", 0, 150).toXmlString();
String xml2 = builder.getTestResult(101, 150, "indexer1", 100, 150).toXmlString();
String xml3 = builder.getTestResult(1, 0, "indexer2", 0, 0).toXmlString();
webServer.enqueue(new MockResponse().setBody(xml1).setHeader("Content-Type", "application/xml; charset=utf-8"));
webServer.enqueue(new MockResponse().setBody(xml2).setHeader("Content-Type", "application/xml; charset=utf-8"));
webServer.enqueue(new MockResponse().setBody(xml3).setHeader("Content-Type", "application/xml; charset=utf-8"));
NewznabParameters apiCallParameters = new NewznabParameters();
apiCallParameters.setApikey("apikey");
apiCallParameters.setOffset(0);
apiCallParameters.setLimit(100);
apiCallParameters.setT(ActionAttribute.SEARCH);
NewznabXmlRoot apiSearchResult = (NewznabXmlRoot) externalApi.api(apiCallParameters).getBody();
assertThat(apiSearchResult.getRssChannel().getItems().size()).isEqualTo(100);
apiCallParameters.setLimit(100);
apiCallParameters.setOffset(100);
apiSearchResult = (NewznabXmlRoot) externalApi.api(apiCallParameters).getBody();
assertThat(apiSearchResult.getRssChannel().getItems().size()).isEqualTo(50);
}
use of org.nzbhydra.mapping.newznab.NewznabParameters in project nzbhydra2 by theotherp.
the class ExternalApiTest method shouldCacheRemoveEntriesWhenLimitReached.
@Test
public void shouldCacheRemoveEntriesWhenLimitReached() throws Exception {
NewznabParameters parameters = getNewznabParameters("q1");
testee.api(parameters);
verify(searcher).search(any());
testee.api(parameters);
verify(searcher, times(1)).search(any());
parameters.setQ("q2");
testee.api(getNewznabParameters("q2"));
verify(searcher, times(2)).search(any());
parameters.setQ("q3");
testee.api(getNewznabParameters("q3"));
verify(searcher, times(3)).search(any());
parameters.setQ("q4");
testee.api(getNewznabParameters("q4"));
verify(searcher, times(4)).search(any());
parameters.setQ("q5");
testee.api(getNewznabParameters("q5"));
verify(searcher, times(5)).search(any());
// q1 is still cached
testee.api(getNewznabParameters("q1"));
verify(searcher, times(5)).search(any());
// now q1 is removed as oldest entry
testee.api(getNewznabParameters("q6"));
verify(searcher, times(6)).search(any());
// Not cached anymore, will do another search
testee.api(getNewznabParameters("q1"));
verify(searcher, times(7)).search(any());
}
Aggregations