Skip to content

Commit

Permalink
fix(cache): evict clan cache after changes to entity/related entities
Browse files Browse the repository at this point in the history
 Closes #812
  • Loading branch information
Ivan-Shaml authored and Brutus5000 committed Dec 27, 2024
1 parent 91159bf commit fbfbd04
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/com/faforever/api/data/domain/Clan.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,7 +35,7 @@
@CreatePermission(expression = Prefab.ALL)
@Setter
@IsLeaderInClan
@EntityListeners(ClanEnricherListener.class)
@EntityListeners({ClanEnricherListener.class, ClanChangeListener.class})
public class Clan extends AbstractEntity<Clan> implements OwnableEntity {

public static final String TYPE_NAME = "clan";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +23,7 @@
@DeletePermission(expression = IsClanMembershipDeletable.EXPRESSION)
@UpdatePermission(expression = IsClanMembershipDeletable.EXPRESSION)
@Setter
@EntityListeners(ClanMembershipChangeListener.class)
public class ClanMembership extends AbstractEntity<ClanMembership> {

private Clan clan;
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit fbfbd04

Please sign in to comment.