-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
z15lross
committed
May 6, 2022
1 parent
2d897b8
commit 2837118
Showing
98 changed files
with
7,521 additions
and
381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
code/api/api/src/main/java/com/decathlon/ara/cache/CacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.decathlon.ara.cache; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import org.springframework.cache.Cache; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.ehcache.EhCacheCache; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.support.TransactionSynchronization; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import com.decathlon.ara.domain.Project; | ||
|
||
@Service | ||
public class CacheService { | ||
|
||
private static final String USER_PROJECTS_CACHE_NAME = "security.user.projects"; | ||
private static final String USER_PROJECT_ROLES_CACHE_NAME = "security.user.project.roles"; | ||
|
||
private CacheManager cacheManager; | ||
|
||
public CacheService(CacheManager cacheManager) { | ||
this.cacheManager = cacheManager; | ||
} | ||
|
||
public void evictCaches(Project project) { | ||
evictCacheAfterCommit(() -> { | ||
evictCache(USER_PROJECT_ROLES_CACHE_NAME, key -> key.toString().startsWith(project.getCode())); | ||
|
||
Cache cache = cacheManager.getCache(USER_PROJECTS_CACHE_NAME); | ||
if (cache != null) { | ||
cache.clear(); | ||
} | ||
}); | ||
} | ||
|
||
public void evictCaches(Project project, String userName) { | ||
evictsUserProjectRolesCache(project, userName); | ||
evictsUserProjectsCache(userName); | ||
} | ||
|
||
public void evictsUserProjectRolesCache(Project project, String userName) { | ||
evictCacheAfterCommit(() -> { | ||
Cache cache = cacheManager.getCache(USER_PROJECT_ROLES_CACHE_NAME); | ||
if (cache != null) { | ||
cache.evict(project.getCode().concat(userName)); | ||
} | ||
}); | ||
} | ||
|
||
public void evictsUserProjectRolesCache(String userName) { | ||
evictCacheAfterCommit(() -> evictCache(USER_PROJECT_ROLES_CACHE_NAME, key -> key.toString().endsWith(userName))); | ||
} | ||
|
||
public void evictsUserProjectsCache(String userName) { | ||
evictCacheAfterCommit(() -> { | ||
Cache cache = cacheManager.getCache(USER_PROJECTS_CACHE_NAME); | ||
if (cache != null) { | ||
cache.evict(userName); | ||
} | ||
}); | ||
} | ||
|
||
public void evictCaches(String userName) { | ||
evictsUserProjectRolesCache(userName); | ||
evictsUserProjectsCache(userName); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void evictCache(String cacheName, Predicate<Object> keyPredicate) { | ||
Cache cache = cacheManager.getCache(cacheName); | ||
if (cache != null) { | ||
if (cache instanceof EhCacheCache ehCacheCache) { | ||
ehCacheCache.getNativeCache().getKeys().stream().filter(keyPredicate).forEach(cache::evict); | ||
} else { | ||
cache.clear(); | ||
} | ||
} | ||
} | ||
|
||
private void evictCacheAfterCommit(Runnable evictRunnable) { | ||
if (TransactionSynchronizationManager.isSynchronizationActive()) { | ||
TransactionSynchronizationManager.registerSynchronization(new EvictCacheSynchronization(evictRunnable)); | ||
} else { | ||
evictRunnable.run(); | ||
} | ||
} | ||
|
||
private static class EvictCacheSynchronization implements TransactionSynchronization { | ||
|
||
private Runnable evictRunnable; | ||
|
||
public EvictCacheSynchronization(Runnable evictRunnable) { | ||
this.evictRunnable = evictRunnable; | ||
} | ||
|
||
@Override | ||
public void afterCommit() { | ||
evictRunnable.run(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.