Search in sources :

Example 1 with NewznabParameters

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);
}
Also used : java.util(java.util) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) InvalidSearchResultIdException(org.nzbhydra.downloading.InvalidSearchResultIdException) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) SearchRequestFactory(org.nzbhydra.searching.searchrequests.SearchRequestFactory) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) org.nzbhydra.mapping.newznab.caps(org.nzbhydra.mapping.newznab.caps) SearchSource(org.nzbhydra.searching.searchrequests.SearchRequest.SearchSource) ConfigProvider(org.nzbhydra.config.ConfigProvider) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) Searcher(org.nzbhydra.searching.Searcher) NewznabParameters(org.nzbhydra.mapping.newznab.NewznabParameters) SearchRequest(org.nzbhydra.searching.searchrequests.SearchRequest) CategoriesConfig(org.nzbhydra.config.CategoriesConfig) Logger(org.slf4j.Logger) NewznabXmlError(org.nzbhydra.mapping.newznab.xml.NewznabXmlError) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Throwables(com.google.common.base.Throwables) IdType(org.nzbhydra.mediainfo.InfoProvider.IdType) OutputType(org.nzbhydra.mapping.newznab.OutputType) DownloadResult(org.nzbhydra.downloading.DownloadResult) SearchResult(org.nzbhydra.searching.SearchResult) SessionStorage(org.nzbhydra.web.SessionStorage) Instant(java.time.Instant) RestController(org.springframework.web.bind.annotation.RestController) ActionAttribute(org.nzbhydra.mapping.newznab.ActionAttribute) CategoryProvider(org.nzbhydra.searching.CategoryProvider) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) FileHandler(org.nzbhydra.downloading.FileHandler) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) SearchType(org.nzbhydra.searching.SearchType) Data(lombok.Data) Entry(java.util.Map.Entry) Clock(java.time.Clock) ResponseEntity(org.springframework.http.ResponseEntity) AllArgsConstructor(lombok.AllArgsConstructor) LoggingMarkers(org.nzbhydra.logging.LoggingMarkers) NewznabResponse(org.nzbhydra.mapping.newznab.NewznabResponse) ResponseEntity(org.springframework.http.ResponseEntity) Entry(java.util.Map.Entry) Instant(java.time.Instant) NewznabResponse(org.nzbhydra.mapping.newznab.NewznabResponse)

Example 2 with NewznabParameters

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;
}
Also used : NewznabParameters(org.nzbhydra.mapping.newznab.NewznabParameters)

Example 3 with NewznabParameters

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());
}
Also used : NewznabParameters(org.nzbhydra.mapping.newznab.NewznabParameters) Test(org.junit.Test)

Example 4 with NewznabParameters

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);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) NewznabXmlRoot(org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot) NewznabResponseBuilder(org.nzbhydra.fortests.NewznabResponseBuilder) NewznabParameters(org.nzbhydra.mapping.newznab.NewznabParameters) Test(org.junit.Test) AbstractConfigReplacingTest(org.nzbhydra.tests.AbstractConfigReplacingTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with NewznabParameters

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());
}
Also used : NewznabParameters(org.nzbhydra.mapping.newznab.NewznabParameters) Test(org.junit.Test)

Aggregations

NewznabParameters (org.nzbhydra.mapping.newznab.NewznabParameters)9 Test (org.junit.Test)7 NewznabXmlRoot (org.nzbhydra.mapping.newznab.xml.NewznabXmlRoot)4 MockResponse (okhttp3.mockwebserver.MockResponse)3 AbstractConfigReplacingTest (org.nzbhydra.tests.AbstractConfigReplacingTest)3 NewznabResponseBuilder (org.nzbhydra.fortests.NewznabResponseBuilder)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Stopwatch (com.google.common.base.Stopwatch)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 Clock (java.time.Clock)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 Stream (java.util.stream.Stream)1 AllArgsConstructor (lombok.AllArgsConstructor)1