This commit is contained in:
@@ -2,10 +2,12 @@ package ru.soune.nocopy;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
@EnableCaching
|
||||
public class NoCopyApplication {
|
||||
|
||||
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 ru.soune.nocopy.dto.BaseRequest;
|
||||
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.GlobalSearchStatisticsResponse;
|
||||
import ru.soune.nocopy.entity.company.Company;
|
||||
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.UserRepository;
|
||||
import ru.soune.nocopy.service.CompanyService;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user