Skip to content

Commit

Permalink
Merge pull request #693 from Onlineberatung/CONNECTA-179-fix-performa…
Browse files Browse the repository at this point in the history
…nce-issue-by-introducing-caching

Connecta 179 fix performance issue by introducing caching
  • Loading branch information
tkuzynow authored Apr 10, 2024
2 parents 2e5c4aa + f100116 commit 4498388
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private Map<String, Object> findByDbConsultant(Consultant dbConsultant) {
var userMap = new HashMap<String, Object>();

messageClient
.findUser(dbConsultant.getRocketChatId())
.findUserAndAddToCache(dbConsultant.getRocketChatId())
.ifPresentOrElse(
chatUserMap -> userMap.putAll(userServiceMapper.mapOf(dbConsultant, chatUserMap)),
throwPersistenceConflict(dbConsultant.getId(), dbConsultant.getRocketChatId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -309,6 +310,12 @@ public Optional<Map<String, Object>> findUser(String chatUserId) {
}
}

@Override
@Cacheable(key = "#chatUserId", value = "rocketChatUserCache")
public Optional<Map<String, Object>> findUserAndAddToCache(String chatUserId) {
return findUser(chatUserId);
}

@Override
public Optional<Map<String, Object>> getChatInfo(String roomId) {
var url = rocketChatConfig.getApiUrl(ENDPOINT_ROOM_INFO + roomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class CacheManagerConfig {
public static final String TENANT_ADMIN_CACHE = "tenantAdminCache";
public static final String TOPICS_CACHE = "topicsCache";

public static final String ROCKET_CHAT_USER_CACHE = "rocketChatUserCache";

@Value("${cache.agencies.configuration.maxEntriesLocalHeap}")
private long agenciesMaxEntriesLocalHeap;

Expand Down Expand Up @@ -80,6 +82,18 @@ public class CacheManagerConfig {
@Value("${cache.appsettings.configuration.timeToLiveSeconds}")
private long appSettingsTimeToLiveSeconds;

@Value("${cache.rocketchat.configuration.maxEntriesLocalHeap}")
private long rocketchatCacheMaxEntriesLocalHeap;

@Value("${cache.rocketchat.configuration.eternal}")
private boolean rocketchatCacheEternal;

@Value("${cache.rocketchat.configuration.timeToIdleSeconds}")
private long rocketchatCacheTimeToIdleSeconds;

@Value("${cache.rocketchat.configuration.timeToLiveSeconds}")
private long rocketchatCacheTimeToLiveSeconds;

@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
Expand All @@ -94,6 +108,8 @@ public net.sf.ehcache.CacheManager ehCacheManager() {
config.addCache(buildTenantAdminCacheConfiguration());
config.addCache(buildTopicCacheConfiguration());
config.addCache(buildApplicationSettingsCacheConfiguration());

config.addCache(buildRocketchatUserCacheConfiguration());
return net.sf.ehcache.CacheManager.newInstance(config);
}

Expand Down Expand Up @@ -156,4 +172,14 @@ private CacheConfiguration buildApplicationSettingsCacheConfiguration() {
appSettingsCacheConfiguration.setTimeToLiveSeconds(appSettingsTimeToLiveSeconds);
return appSettingsCacheConfiguration;
}

private CacheConfiguration buildRocketchatUserCacheConfiguration() {
var rocketchatCacheConfiguration = new CacheConfiguration();
rocketchatCacheConfiguration.setName(ROCKET_CHAT_USER_CACHE);
rocketchatCacheConfiguration.setMaxEntriesLocalHeap(rocketchatCacheMaxEntriesLocalHeap);
rocketchatCacheConfiguration.setEternal(rocketchatCacheEternal);
rocketchatCacheConfiguration.setTimeToIdleSeconds(rocketchatCacheTimeToIdleSeconds);
rocketchatCacheConfiguration.setTimeToLiveSeconds(rocketchatCacheTimeToLiveSeconds);
return rocketchatCacheConfiguration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface MessageClient {

Optional<Map<String, Object>> findUser(String chatUserId);

Optional<Map<String, Object>> findUserAndAddToCache(String chatUserId);

Optional<List<Map<String, String>>> findAllChats(String chatUserId);

boolean updateChatE2eKey(String chatUserId, String roomId, String key);
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ cache.appsettings.configuration.eternal=false
cache.appsettings.configuration.timeToIdleSeconds=0
cache.appsettings.configuration.timeToLiveSeconds=60


cache.rocketchat.configuration.maxEntriesLocalHeap=100
cache.rocketchat.configuration.eternal=false
cache.rocketchat.configuration.timeToIdleSeconds=0
cache.rocketchat.configuration.timeToLiveSeconds=900

# MailService API
mail.service.api.url=http://mailservice:8080/service

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -154,6 +155,8 @@ class UserControllerChatE2EIT {

@MockBean private AgencyServiceApiControllerFactory agencyServiceApiControllerFactory;

@Autowired private CacheManager cacheManager;

@MockBean
@Qualifier("restTemplate")
private RestTemplate restTemplate;
Expand Down Expand Up @@ -207,6 +210,7 @@ void reset() {
subscriptionsGetResponse = null;
groupDeleteResponse = null;
identityConfig.setDisplayNameAllowedForConsultants(false);
cacheManager.getCache("rocketChatUserCache").clear();
}

@BeforeEach
Expand Down

0 comments on commit 4498388

Please sign in to comment.