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
View File
@@ -77,6 +77,10 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.0' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.0'
implementation 'io.github.resilience4j:resilience4j-spring-boot3:2.2.0' implementation 'io.github.resilience4j:resilience4j-spring-boot3:2.2.0'
//cache
implementation("org.springframework.boot:spring-boot-starter-cache:4.0.6")
implementation 'com.github.ben-manes.caffeine:caffeine'
} }
tasks.named('test') { tasks.named('test') {
@@ -2,10 +2,12 @@ package ru.soune.nocopy;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
@EnableCaching
public class NoCopyApplication { public class NoCopyApplication {
public static void main(String[] args) { public static void main(String[] args) {
@@ -0,0 +1,30 @@
package ru.soune.nocopy.configuration;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager(
"imageSearch", "parsedImages");
cacheManager.setCaffeine(Caffeine.newBuilder()
.maximumSize(10000)
.expireAfterWrite(1, TimeUnit.HOURS)
.recordStats()
);
return cacheManager;
}
}
@@ -6,21 +6,12 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ru.soune.nocopy.dto.BaseRequest; import ru.soune.nocopy.dto.BaseRequest;
import ru.soune.nocopy.dto.BaseResponse; import ru.soune.nocopy.dto.BaseResponse;
import ru.soune.nocopy.dto.MessageCode;
import ru.soune.nocopy.dto.search.GlobalSearchStatisticsRequest; import ru.soune.nocopy.dto.search.GlobalSearchStatisticsRequest;
import ru.soune.nocopy.dto.search.GlobalSearchStatisticsResponse; import ru.soune.nocopy.dto.search.GlobalSearchStatisticsResponse;
import ru.soune.nocopy.entity.company.Company;
import ru.soune.nocopy.entity.user.AuthToken; import ru.soune.nocopy.entity.user.AuthToken;
import ru.soune.nocopy.entity.user.User;
import ru.soune.nocopy.repository.AuthTokenRepository; import ru.soune.nocopy.repository.AuthTokenRepository;
import ru.soune.nocopy.repository.UserRepository;
import ru.soune.nocopy.service.CompanyService;
import ru.soune.nocopy.service.search.GlobalSearchStatisticsService; import ru.soune.nocopy.service.search.GlobalSearchStatisticsService;
import ru.soune.nocopy.service.user.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
@Slf4j @Slf4j
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import org.springframework.cache.annotation.Cacheable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*; import okhttp3.*;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@@ -24,18 +26,13 @@ import java.util.concurrent.*;
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor
public class SearchImageService { public class SearchImageService {
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
private final SearchProperties searchProperties; private final SearchProperties searchProperties;
public SearchImageService(SearchProperties searchProperties) {
this.searchProperties = searchProperties;
this.objectMapper = new ObjectMapper();
}
@Value("${yandex.api-key}") @Value("${yandex.api-key}")
private String apiKey; private String apiKey;
@@ -57,6 +54,11 @@ public class SearchImageService {
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 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) public String searchReverseByPublicUrl(FileEntity fileEntity, String engine, String searchType)
throws IOException, TimeoutException { 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) public List<YandexSearchResponse.ImageResult> getAllImagesWithoutPagination(String searchApiJson, String findType)
throws IOException { throws IOException {
JsonNode root = objectMapper.readTree(searchApiJson); JsonNode root = objectMapper.readTree(searchApiJson);