Skip to content

Commit

Permalink
added caching
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovychh committed Feb 29, 2024
1 parent 1cee1dc commit caa7b1a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.app.sellerretailreports.cache;

import java.util.List;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.stereotype.Component;

@Component
public class SpringCacheCustomizer implements CacheManagerCustomizer<ConcurrentMapCacheManager> {
@Override
public void customize(ConcurrentMapCacheManager cacheManager) {
cacheManager.setCacheNames(List.of(
"salesAndTrafficByDatesBetween",
"salesAndTrafficBySpecifiedDate",
"salesAndTrafficBySpecifiedAsins",
"salesAndTrafficByAsins",
"salesAndTrafficByDates"
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.app.sellerretailreports.cache;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class SpringCachingConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -21,7 +23,7 @@ public class SalesAndTrafficByDateServiceImpl implements SalesAndTrafficByDateSe
private final SalesAndTrafficByAsinMapper salesAndTrafficByAsinMapper;

@Override
@Cacheable("salesAndTrafficByDateDto")
@Cacheable("salesAndTrafficByDatesBetween")
public List<SalesAndTrafficByDateDto> findAllBetweenDates(LocalDate start, LocalDate end) {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
return salesAndTrafficReport.getSalesAndTrafficByDate().stream()
Expand All @@ -32,6 +34,7 @@ public List<SalesAndTrafficByDateDto> findAllBetweenDates(LocalDate start, Local
}

@Override
@Cacheable("salesAndTrafficBySpecifiedDate")
public SalesAndTrafficByDateDto findBySpecifiedDate(LocalDate localDate) {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
SalesAndTrafficByDate salesAndTrafficByDate = salesAndTrafficReport
Expand All @@ -45,7 +48,7 @@ public SalesAndTrafficByDateDto findBySpecifiedDate(LocalDate localDate) {
}

@Override
@Cacheable("salesAndTrafficByAsinDto")
@Cacheable("salesAndTrafficBySpecifiedAsins")
public List<SalesAndTrafficByAsinDto> findAllBySpecifiedAsins(List<String> asins) {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
return salesAndTrafficReport.getSalesAndTrafficByAsin().stream()
Expand All @@ -55,7 +58,7 @@ public List<SalesAndTrafficByAsinDto> findAllBySpecifiedAsins(List<String> asins
}

@Override
@Cacheable("salesAndTrafficByAsinDto")
@Cacheable("salesAndTrafficByAsins")
public List<SalesAndTrafficByAsinDto> findAllByAsins() {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
return salesAndTrafficReport.getSalesAndTrafficByAsin().stream()
Expand All @@ -64,10 +67,25 @@ public List<SalesAndTrafficByAsinDto> findAllByAsins() {
}

@Override
@Cacheable("salesAndTrafficByDates")
public List<SalesAndTrafficByDateDto> findAllByDates() {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
return salesAndTrafficReport.getSalesAndTrafficByDate().stream()
.map(salesAndTrafficByDateMapper::toDto)
.toList();
}

@CacheEvict(
value = {
"salesAndTrafficByDatesBetween",
"salesAndTrafficBySpecifiedDate",
"salesAndTrafficBySpecifiedAsins",
"salesAndTrafficByAsins",
"salesAndTrafficByDates"
},
allEntries = true
)
@Scheduled(fixedRateString = "${caching.spring.TTL}")
public void emptyHotelsCache() {
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.password=${MANGODB_PASSWORD}
spring.data.mongodb.database=report_db
caching.spring.TTL=420000

jwt.expiration=${JWT_EXPIRATION}
jwt.secret=${JWT_SECRET}

0 comments on commit caa7b1a

Please sign in to comment.