dev add cache for search
Test Workflow / test (push) Has been cancelled

This commit is contained in:
2026-05-21 16:06:47 +07:00
parent ab796932e6
commit cf2aefe6c3
5 changed files with 50 additions and 15 deletions
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import jakarta.annotation.PostConstruct;
import org.springframework.cache.annotation.Cacheable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
@@ -24,18 +26,13 @@ import java.util.concurrent.*;
@Slf4j
@Service
@RequiredArgsConstructor
public class SearchImageService {
private final ObjectMapper objectMapper;
private final SearchProperties searchProperties;
public SearchImageService(SearchProperties searchProperties) {
this.searchProperties = searchProperties;
this.objectMapper = new ObjectMapper();
}
@Value("${yandex.api-key}")
private String apiKey;
@@ -57,6 +54,11 @@ public class SearchImageService {
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
@Cacheable(
value = "reverseImageSearch",
key = "#fileEntity.id + '_' + #engine + '_' + #searchType",
unless = "#result == null || #result.isEmpty()"
)
public String searchReverseByPublicUrl(FileEntity fileEntity, String engine, String searchType)
throws IOException, TimeoutException {
@@ -153,6 +155,12 @@ public class SearchImageService {
}
}
@Cacheable(
value = "parsedImages",
key = "#searchApiJson + '_' + #findType",
condition = "#searchApiJson != null && #searchApiJson.length() > 0",
unless = "#result == null || #result.isEmpty()"
)
public List<YandexSearchResponse.ImageResult> getAllImagesWithoutPagination(String searchApiJson, String findType)
throws IOException {
JsonNode root = objectMapper.readTree(searchApiJson);