Skip to content

Commit

Permalink
fixes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliL committed Mar 10, 2024
1 parent 4becae0 commit 10b3f66
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class CacheNames {
public static final String GROUP_CAPITALSOURCES_BY_DATE = "groupCapitalsourcesByDate";
public static final String CAPITALSOURCE_BY_ID = "capitalsourceById";
public static final String ALL_CONTRACTPARTNER = "allContractpartner";
public static final String ALL_CONTRACTPARTNER_BY_DATE = "allContractpartnerByDate";
public static final String CONTRACTPARTNER_BY_ID = "contractpartnerById";
public static final String ALL_PRE_DEF_MONEYFLOWS = "allPreDefMoneyflows";
public static final String PRE_DEF_MONEYFLOW_BY_ID = "preDefMoneyflowById";
public static final String MONEYFLOW_BY_ID = "moneyflowById";
public static final String MONEYFLOW_YEARS = "moneyflowYears";
public static final String MONEYFLOW_MONTH = "moneyflowMonths";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

import org.laladev.moneyjinn.service.dao.data.MoneyflowData;
Expand Down Expand Up @@ -66,7 +67,8 @@ public List<Integer> getAllYears(final Long userId) {
}

public List<Integer> getAllMonth(final Long userId, final LocalDate beginOfYear, final LocalDate endOfYear) {
return this.mapper.getAllMonth(userId, beginOfYear, endOfYear);
final List<Integer> allMonth = this.mapper.getAllMonth(userId, beginOfYear, endOfYear);
return allMonth == null ? Collections.emptyList() : allMonth;
}

public BigDecimal getSumAmountByDateRangeForCapitalsourceIds(final Long userId, final LocalDate validFrom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
package org.laladev.moneyjinn.service.impl;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

import org.laladev.moneyjinn.core.mapper.AbstractMapperSupport;
import org.springframework.cache.Cache;
Expand All @@ -44,9 +47,49 @@ public abstract class AbstractService extends AbstractMapperSupport {

static final LocalDate MAX_DATE = LocalDate.parse("2999-12-31");

protected Cache getCache(final String... cacheNameParts) {
final String cacheName = String.join("#", cacheNameParts);
return this.cacheManager.getCache(cacheName);
protected String getCombinedCacheName(final Object... cacheNameParts) {
return String.join("#", Arrays.toString(cacheNameParts));
}

protected void evictFromCache(final String cacheName, final Object key) {
final var cache = this.cacheManager.getCache(cacheName);
if (cache != null)
cache.evict(key);
}

protected void clearCache(final String cacheName) {
final var cache = this.cacheManager.getCache(cacheName);
if (cache != null)
cache.clear();
}

protected <T> T getFromCacheOrExecute(final String cacheName, final Object key, final Supplier<T> supplier,
final Class<T> clazz) {
final Cache cache = this.cacheManager.getCache(cacheName);

final T cacheValue = cache.get(key, clazz);
if (cacheValue != null) {
return cacheValue;
}

final T value = supplier.get();
cache.put(key, value);
return value;
}

protected <T> List<T> getListFromCacheOrExecute(final String cacheName, final Object key,
final Supplier<List<T>> supplier, final Class<T> clazz) {
final Cache cache = this.cacheManager.getCache(cacheName);

@SuppressWarnings("unchecked")
final List<T> cacheValue = cache.get(key, List.class);
if (cacheValue != null) {
return cacheValue;
}

final List<T> value = supplier.get();
cache.put(key, value);
return value;
}

protected void publishEvent(final ApplicationEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.laladev.moneyjinn.core.error.ErrorCode;
Expand All @@ -49,7 +50,6 @@
import org.laladev.moneyjinn.service.dao.AccessRelationDao;
import org.laladev.moneyjinn.service.dao.data.AccessRelationData;
import org.laladev.moneyjinn.service.dao.data.mapper.AccessRelationDataMapper;
import org.springframework.cache.Cache;
import org.springframework.util.Assert;

import jakarta.annotation.PostConstruct;
Expand Down Expand Up @@ -113,19 +113,15 @@ public Group getGroup(final UserID userId, final LocalDate date) {
return null;
}

@SuppressWarnings("unchecked")
@Override
public List<AccessRelation> getAllAccessRelationsById(final UserID userId) {
Assert.notNull(userId, USER_ID_MUST_NOT_BE_NULL);
final Cache cache = super.getCache(CacheNames.ALL_ACCESS_RELATIONS_BY_USER_ID);
List<AccessRelation> result = cache.get(userId.getId(), List.class);
if (result == null) {
final List<AccessRelationData> accessRelationDataList = this.accessRelationDao
.getAllAccessRelationsById(userId.getId());
result = super.mapList(accessRelationDataList, AccessRelation.class);
cache.put(userId.getId(), result);
}
return result;

final Supplier<List<AccessRelation>> supplier = () -> super.mapList(
this.accessRelationDao.getAllAccessRelationsById(userId.getId()), AccessRelation.class);

return super.getListFromCacheOrExecute(CacheNames.ALL_ACCESS_RELATIONS_BY_USER_ID, userId, supplier,
AccessRelation.class);
}

@Override
Expand Down Expand Up @@ -314,9 +310,8 @@ private void setAccessRelation(final AccessRelation accessRelation) {
}
}

private void evictAccessRelationCache(final AccessID accessId) {
final Cache cache = super.getCache(CacheNames.ALL_ACCESS_RELATIONS_BY_USER_ID);
cache.evict(accessId.getId());
private void evictAccessRelationCache(final UserID userId) {
super.evictFromCache(CacheNames.ALL_ACCESS_RELATIONS_BY_USER_ID, userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -58,8 +58,6 @@
import org.laladev.moneyjinn.service.dao.data.mapper.CapitalsourceDataMapper;
import org.laladev.moneyjinn.service.event.CapitalsourceChangedEvent;
import org.laladev.moneyjinn.service.event.EventType;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.interceptor.SimpleKey;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -170,24 +168,27 @@ public ValidationResult validateCapitalsource(final Capitalsource capitalsource)
}

@Override
@Cacheable(CacheNames.CAPITALSOURCE_BY_ID)
public Capitalsource getCapitalsourceById(final UserID userId, final GroupID groupId,
final CapitalsourceID capitalsourceId) {
Assert.notNull(userId, USER_ID_MUST_NOT_BE_NULL);
Assert.notNull(groupId, "GroupId must not be null!");
Assert.notNull(capitalsourceId, "CapitalsourceId must not be null!");
final CapitalsourceData capitalsourceData = this.capitalsourceDao.getCapitalsourceById(userId.getId(),
groupId.getId(), capitalsourceId.getId());
return this.mapCapitalsourceData(capitalsourceData);

final Supplier<Capitalsource> supplier = () -> this.mapCapitalsourceData(
this.capitalsourceDao.getCapitalsourceById(userId.getId(), groupId.getId(), capitalsourceId.getId()));

return super.getFromCacheOrExecute(CacheNames.CAPITALSOURCE_BY_ID,
new SimpleKey(userId, groupId, capitalsourceId), supplier, Capitalsource.class);
}

@Override
@Cacheable(CacheNames.ALL_CAPITALSOURCES)
public List<Capitalsource> getAllCapitalsources(final UserID userId) {
Assert.notNull(userId, USER_ID_MUST_NOT_BE_NULL);
final List<CapitalsourceData> capitalsourceDataList = this.capitalsourceDao
.getAllCapitalsources(userId.getId());
return this.mapCapitalsourceDataList(capitalsourceDataList);

final Supplier<List<Capitalsource>> supplier = () -> this
.mapCapitalsourceDataList(this.capitalsourceDao.getAllCapitalsources(userId.getId()));

return super.getListFromCacheOrExecute(CacheNames.ALL_CAPITALSOURCES, userId, supplier, Capitalsource.class);
}

@Override
Expand Down Expand Up @@ -283,23 +284,19 @@ public List<Capitalsource> getGroupBookableCapitalsourcesByDateRange(final UserI
return capitalsources.stream().filter(cs -> !cs.getType().equals(CapitalsourceType.CREDIT)).toList();
}

@SuppressWarnings("unchecked")
@Override
public List<Capitalsource> getGroupCapitalsourcesByDateRange(final UserID userId, final LocalDate validFrom,
final LocalDate validTil) {
Assert.notNull(userId, USER_ID_MUST_NOT_BE_NULL);
Assert.notNull(validFrom, "ValidFrom must not be null!");
Assert.notNull(validTil, "ValidTil must not be null!");
final Cache cache = super.getCache(CacheNames.GROUP_CAPITALSOURCES_BY_DATE, userId.getId().toString());
final SimpleKey key = new SimpleKey(validFrom, validTil);
List<Capitalsource> capitalsources = cache.get(key, List.class);
if (capitalsources == null) {
final List<CapitalsourceData> capitalsourceDataList = this.capitalsourceDao
.getGroupCapitalsourcesByDateRange(userId.getId(), validFrom, validTil);
capitalsources = this.mapCapitalsourceDataList(capitalsourceDataList);
cache.put(key, capitalsources);
}
return capitalsources;

final Supplier<List<Capitalsource>> supplier = () -> this.mapCapitalsourceDataList(
this.capitalsourceDao.getGroupCapitalsourcesByDateRange(userId.getId(), validFrom, validTil));

return super.getListFromCacheOrExecute(
super.getCombinedCacheName(CacheNames.GROUP_CAPITALSOURCES_BY_DATE, userId.getId()),
new SimpleKey(validFrom, validTil), supplier, Capitalsource.class);
}

@Override
Expand All @@ -313,22 +310,13 @@ public Capitalsource getCapitalsourceByAccount(final UserID userId, final BankAc
private void evictCapitalsourceCache(final UserID userId, final GroupID groupId,
final CapitalsourceID capitalsourceId) {
if (capitalsourceId != null) {
final Cache allCapitalsourcesCache = super.getCache(CacheNames.ALL_CAPITALSOURCES);
final Cache capitalsourceByIdCache = super.getCache(CacheNames.CAPITALSOURCE_BY_ID);
final Set<UserID> userIds = this.accessRelationService.getAllUserWithSameGroup(userId);
for (final UserID evictingUserId : userIds) {
final Cache groupCapitalsourcesByDateCache = super.getCache(CacheNames.GROUP_CAPITALSOURCES_BY_DATE,
evictingUserId.getId().toString());
if (allCapitalsourcesCache != null) {
allCapitalsourcesCache.evict(evictingUserId);
}
if (capitalsourceByIdCache != null) {
capitalsourceByIdCache.evict(new SimpleKey(evictingUserId, groupId, capitalsourceId));
}
if (groupCapitalsourcesByDateCache != null) {
groupCapitalsourcesByDateCache.clear();
}
}
this.accessRelationService.getAllUserWithSameGroup(userId).forEach(evictingUserId -> {
super.evictFromCache(CacheNames.CAPITALSOURCE_BY_ID,
new SimpleKey(evictingUserId, groupId, capitalsourceId));
super.evictFromCache(CacheNames.ALL_CAPITALSOURCES, evictingUserId);
super.clearCache(
super.getCombinedCacheName(CacheNames.GROUP_CAPITALSOURCES_BY_DATE, evictingUserId.getId()));
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.laladev.moneyjinn.core.error.ErrorCode;
import org.laladev.moneyjinn.model.BankAccount;
Expand All @@ -50,8 +50,6 @@
import org.laladev.moneyjinn.service.dao.data.ContractpartnerAccountData;
import org.laladev.moneyjinn.service.dao.data.mapper.BankAccountDataMapper;
import org.laladev.moneyjinn.service.dao.data.mapper.ContractpartnerAccountDataMapper;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.interceptor.SimpleKey;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -147,25 +145,30 @@ private ContractpartnerAccount getContractpartnerAccountByBankAccount(final User
}

@Override
@Cacheable(CacheNames.CONTRACTPARTNER_ACCOUNT_BY_ID)
public ContractpartnerAccount getContractpartnerAccountById(final UserID userId,
final ContractpartnerAccountID contractpartnerAccountId) {
Assert.notNull(userId, USER_ID_MUST_NOT_BE_NULL);
Assert.notNull(contractpartnerAccountId, "ContractpartnerAccountId must not be null!");
final ContractpartnerAccountData contractpartnerAccountData = this.contractpartnerAccountDao
.getContractpartnerAccountById(contractpartnerAccountId.getId());
return this.mapContractpartnerAccountData(userId, contractpartnerAccountData);

final Supplier<ContractpartnerAccount> supplier = () -> this.mapContractpartnerAccountData(userId,
this.contractpartnerAccountDao.getContractpartnerAccountById(contractpartnerAccountId.getId()));

return super.getFromCacheOrExecute(CacheNames.CONTRACTPARTNER_ACCOUNT_BY_ID,
new SimpleKey(userId, contractpartnerAccountId), supplier, ContractpartnerAccount.class);
}

@Override
@Cacheable(CacheNames.CONTRACTPARTNER_ACCOUNTS_BY_PARTNER)
public List<ContractpartnerAccount> getContractpartnerAccounts(final UserID userId,
final ContractpartnerID contractpartnerId) {
Assert.notNull(userId, USER_ID_MUST_NOT_BE_NULL);
Assert.notNull(contractpartnerId, "ContractpartnerId must not be null!");
final List<ContractpartnerAccountData> contractpartnerAccountData = this.contractpartnerAccountDao
.getContractpartnerAccounts(contractpartnerId.getId());
return this.mapContractpartnerAccountDataList(userId, contractpartnerAccountData);

final Supplier<List<ContractpartnerAccount>> supplier = () -> this.mapContractpartnerAccountDataList(userId,
this.contractpartnerAccountDao.getContractpartnerAccounts(contractpartnerId.getId()));

return super.getListFromCacheOrExecute(CacheNames.CONTRACTPARTNER_ACCOUNTS_BY_PARTNER,
new SimpleKey(userId, contractpartnerId), supplier, ContractpartnerAccount.class);

}

@Override
Expand Down Expand Up @@ -259,18 +262,12 @@ public List<ContractpartnerAccount> getAllContractpartnerByAccounts(final UserID
private void evictContractpartnerAccountCache(final UserID userId,
final ContractpartnerAccountID contractpartnerAccountIDd, final ContractpartnerID contractpartnerId) {
if (contractpartnerAccountIDd != null) {
final Cache contractpartnerAccountsByPartnerCache = super.getCache(
CacheNames.CONTRACTPARTNER_ACCOUNTS_BY_PARTNER);
final Cache contractpartnerAccountByIdCache = super.getCache(CacheNames.CONTRACTPARTNER_ACCOUNT_BY_ID);
final Set<UserID> userIds = this.accessRelationService.getAllUserWithSameGroup(userId);
for (final UserID evictingUserId : userIds) {
if (contractpartnerAccountsByPartnerCache != null) {
contractpartnerAccountsByPartnerCache.evict(new SimpleKey(evictingUserId, contractpartnerId));
}
if (contractpartnerAccountByIdCache != null) {
contractpartnerAccountByIdCache.evict(new SimpleKey(evictingUserId, contractpartnerAccountIDd));
}
}
this.accessRelationService.getAllUserWithSameGroup(userId).forEach(evictingUserId -> {
super.evictFromCache(CacheNames.CONTRACTPARTNER_ACCOUNTS_BY_PARTNER,
new SimpleKey(evictingUserId, contractpartnerId));
super.evictFromCache(CacheNames.CONTRACTPARTNER_ACCOUNT_BY_ID,
new SimpleKey(evictingUserId, contractpartnerAccountIDd));
});
}
}
}
Loading

0 comments on commit 10b3f66

Please sign in to comment.