-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache inspection and eviction services
Added new endpoints for cache inspection and introduced CacheEvictionService to handle cache eviction for users. Security configuration was updated to allow access to cache endpoints, and several services were modified to utilize the new cache eviction mechanisms.
- Loading branch information
Showing
21 changed files
with
176 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This class can be used to interact with the CacheController in the PSAMA API | ||
# The CacheController must be enable in the application.properties file | ||
# You can enable it by setting the following property: | ||
# app.cache.inspect.enabled=true | ||
# If you are using a environment variable to set the property, you can set it like this: | ||
# CACHE_INSPECT_ENABLED=true | ||
|
||
### GET /cache - Get a list of all caches in the application | ||
GET https://dev.picsure.biodatacatalyst.nhlbi.nih.gov/psama/cache | ||
|
||
### GET /cache/{cacheName} - Get the contents of a specific cache | ||
GET https://dev.picsure.biodatacatalyst.nhlbi.nih.gov/psama/cache/mergedRulesCache |
16 changes: 15 additions & 1 deletion
16
...-services/src/main/java/edu/harvard/hms/dbmi/avillach/auth/config/CustomKeyGenerator.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 |
---|---|---|
@@ -1,20 +1,34 @@ | ||
package edu.harvard.hms.dbmi.avillach.auth.config; | ||
|
||
import edu.harvard.hms.dbmi.avillach.auth.entity.User; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cache.interceptor.KeyGenerator; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class CustomKeyGenerator implements KeyGenerator { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(CustomKeyGenerator.class); | ||
|
||
@Override | ||
public Object generate(Object target, Method method, Object... params) { | ||
String key = null; | ||
for (Object param : params) { | ||
if (param instanceof User user) { | ||
return user.getSubject(); | ||
key = user.getSubject(); | ||
} | ||
|
||
if (param instanceof String userSubject) { | ||
key = userSubject; | ||
} | ||
} | ||
|
||
if (key != null) { | ||
logger.info("Generated cache key: {}", key); | ||
return key; | ||
} | ||
|
||
throw new IllegalArgumentException("No valid params found. Cannot generate cache key"); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...-auth-services/src/main/java/edu/harvard/hms/dbmi/avillach/auth/rest/CacheController.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,42 @@ | ||
package edu.harvard.hms.dbmi.avillach.auth.rest; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
import org.springframework.cache.Cache; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Collection; | ||
|
||
@RestController | ||
@ConditionalOnExpression("${app.cache.inspect.enabled:false}") | ||
@RequestMapping("/cache") | ||
public class CacheController { | ||
|
||
private final CacheManager cacheManager; | ||
|
||
@Autowired | ||
public CacheController(CacheManager cacheManager) { | ||
this.cacheManager = cacheManager; | ||
} | ||
|
||
@GetMapping | ||
public Collection<String> getCacheNames() { | ||
return cacheManager.getCacheNames(); | ||
} | ||
|
||
@GetMapping("/{cacheName}") | ||
public Object getCache(@PathVariable("cacheName") String cacheName) { | ||
Cache cache = cacheManager.getCache(cacheName); | ||
if (cache == null) { | ||
throw new IllegalArgumentException("Cache not found: " + cacheName); | ||
} | ||
|
||
return cache.getNativeCache(); | ||
} | ||
|
||
|
||
} |
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
34 changes: 34 additions & 0 deletions
34
...s/src/main/java/edu/harvard/hms/dbmi/avillach/auth/service/impl/CacheEvictionService.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,34 @@ | ||
package edu.harvard.hms.dbmi.avillach.auth.service.impl; | ||
|
||
import edu.harvard.hms.dbmi.avillach.auth.entity.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CacheEvictionService { | ||
|
||
private final SessionService sessionService; | ||
private final UserService userService; | ||
private final AccessRuleService accessRuleService; | ||
|
||
@Autowired | ||
public CacheEvictionService(SessionService sessionService, UserService userService, AccessRuleService accessRuleService) { | ||
this.sessionService = sessionService; | ||
this.userService = userService; | ||
this.accessRuleService = accessRuleService; | ||
} | ||
|
||
public void evictCache(String userSubject) { | ||
this.sessionService.endSession(userSubject); | ||
this.userService.evictFromCache(userSubject); | ||
this.accessRuleService.evictFromMergedAccessRuleCache(userSubject); | ||
this.accessRuleService.evictFromPreProcessedAccessRules(userSubject); | ||
this.userService.removeUserPassport(userSubject); | ||
} | ||
|
||
public void evictCache(User user) { | ||
String userSubject = user.getSubject(); | ||
evictCache(userSubject); | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.