diff --git a/src/main/java/com/faforever/api/data/domain/Clan.java b/src/main/java/com/faforever/api/data/domain/Clan.java index ec01ffab2..0c2ab2664 100644 --- a/src/main/java/com/faforever/api/data/domain/Clan.java +++ b/src/main/java/com/faforever/api/data/domain/Clan.java @@ -2,6 +2,7 @@ import com.faforever.api.data.checks.IsEntityOwner; import com.faforever.api.data.checks.Prefab; +import com.faforever.api.data.listeners.ClanChangeListener; import com.faforever.api.data.listeners.ClanEnricherListener; import com.faforever.api.data.validation.IsLeaderInClan; import com.yahoo.elide.annotation.ComputedAttribute; @@ -34,7 +35,7 @@ @CreatePermission(expression = Prefab.ALL) @Setter @IsLeaderInClan -@EntityListeners(ClanEnricherListener.class) +@EntityListeners({ClanEnricherListener.class, ClanChangeListener.class}) public class Clan extends AbstractEntity implements OwnableEntity { public static final String TYPE_NAME = "clan"; diff --git a/src/main/java/com/faforever/api/data/domain/ClanMembership.java b/src/main/java/com/faforever/api/data/domain/ClanMembership.java index 84c5d8ea4..3a2967ac0 100644 --- a/src/main/java/com/faforever/api/data/domain/ClanMembership.java +++ b/src/main/java/com/faforever/api/data/domain/ClanMembership.java @@ -1,9 +1,13 @@ package com.faforever.api.data.domain; import com.faforever.api.data.checks.IsClanMembershipDeletable; +import com.faforever.api.data.listeners.ClanMembershipChangeListener; import com.yahoo.elide.annotation.DeletePermission; import com.yahoo.elide.annotation.Include; import com.yahoo.elide.annotation.UpdatePermission; + +import jakarta.persistence.EntityListeners; + import lombok.Setter; import jakarta.persistence.Entity; @@ -19,6 +23,7 @@ @DeletePermission(expression = IsClanMembershipDeletable.EXPRESSION) @UpdatePermission(expression = IsClanMembershipDeletable.EXPRESSION) @Setter +@EntityListeners(ClanMembershipChangeListener.class) public class ClanMembership extends AbstractEntity { private Clan clan; diff --git a/src/main/java/com/faforever/api/data/listeners/ClanChangeListener.java b/src/main/java/com/faforever/api/data/listeners/ClanChangeListener.java new file mode 100644 index 000000000..69056743c --- /dev/null +++ b/src/main/java/com/faforever/api/data/listeners/ClanChangeListener.java @@ -0,0 +1,22 @@ +package com.faforever.api.data.listeners; + +import com.faforever.api.data.domain.Clan; + +import jakarta.persistence.PostRemove; +import jakarta.persistence.PostUpdate; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class ClanChangeListener { + + @CacheEvict(cacheNames = Clan.TYPE_NAME, allEntries = true) + @PostUpdate + @PostRemove + public void clanChanged(Clan clan) { + log.debug("Clan cache evicted, due to change on Clan with id: {}", clan.getId()); + } +} diff --git a/src/main/java/com/faforever/api/data/listeners/ClanMembershipChangeListener.java b/src/main/java/com/faforever/api/data/listeners/ClanMembershipChangeListener.java new file mode 100644 index 000000000..c943901ce --- /dev/null +++ b/src/main/java/com/faforever/api/data/listeners/ClanMembershipChangeListener.java @@ -0,0 +1,26 @@ +package com.faforever.api.data.listeners; + +import com.faforever.api.data.domain.Clan; +import com.faforever.api.data.domain.ClanMembership; + +import jakarta.persistence.PostPersist; +import jakarta.persistence.PostRemove; +import jakarta.persistence.PostUpdate; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class ClanMembershipChangeListener { + + @CacheEvict(cacheNames = Clan.TYPE_NAME, allEntries = true) + @PostUpdate + @PostRemove + @PostPersist + public void clanMembershipChanged(ClanMembership clanMembership) { + log.debug("Clan cache evicted, due to change on ClanMembership with id: {}", + clanMembership.getId()); + } +}