diff --git a/src/main/java/com/example/jonebook/services/search/MemCachedSearchEmployee.java b/src/main/java/com/example/jonebook/services/search/MemCachedSearchEmployee.java index b5feb76..35d5489 100644 --- a/src/main/java/com/example/jonebook/services/search/MemCachedSearchEmployee.java +++ b/src/main/java/com/example/jonebook/services/search/MemCachedSearchEmployee.java @@ -1,5 +1,7 @@ package com.example.jonebook.services.search; +import com.example.jonebook.entities.Employee; +import com.example.jonebook.services.dto.EmployeeCriteria; import java.util.HashMap; import java.util.Map; import org.springframework.data.domain.Page; @@ -8,26 +10,23 @@ import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; -import com.example.jonebook.entities.Employee; -import com.example.jonebook.services.dto.EmployeeCriteria; - @Service public class MemCachedSearchEmployee implements SearchEmployee { - private final SearchEmployee origin; - private final Map, Page> cache = new HashMap<>(); + private final SearchEmployee origin; + private final Map, Page> cache = + new HashMap<>(); - public MemCachedSearchEmployee(SearchEmployee origin) { - this.origin = origin; - } + public MemCachedSearchEmployee(SearchEmployee origin) { + this.origin = origin; + } - @Override - public Page search(@NonNull EmployeeCriteria criteria, @NonNull Pageable pageable) { - if (cache.size() > 10000) - cache.keySet().stream().findFirst().ifPresent(x -> cache.remove(x)); - var key = Pair.of(criteria, pageable); - if (!cache.containsKey(key)) - cache.put(key, origin.search(criteria, pageable)); - return cache.get(key); - } + @Override + public Page search(@NonNull EmployeeCriteria criteria, + @NonNull Pageable pageable) { + if (cache.size() > 10000) + cache.keySet().stream().findFirst().ifPresent(cache::remove); + var key = Pair.of(criteria, pageable); + return cache.computeIfAbsent(key, x -> origin.search(criteria, pageable)); + } }