gpt4 book ai didi

java - 我怎样才能在应用程序的任何部分从 shiro 框架中获取 cacheManager

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:26:06 26 4
gpt4 key购买 nike

如何在我的应用程序的任何部分获取对 Shiro 框架中的 cacheManager 对象的引用?例如,我想删除在删除用户或更新其权限期间缓存的旧用户数据。现在我正在按照以下方式处理它

public void cleanUserCache(final String userName) {
final EmbeddedCacheManager embeddedCacheManager = securityRealmsProducer.getEmbeddedCacheManger();
final Cache<Object, Object> authenticationCache = embeddedCacheManager.getCache("JPA-Auth-Realm.authenticationCache");
final Cache<Object, Object> authrizationCache = embeddedCacheManager.getCache("JPA-Auth-Realm.authorizationCache");
final Object userAuthenticationInfo = authenticationCache.get(userName);
if (userAuthenticationInfo != null) {
authenticationCache.remove(userName);
final SimpleAuthenticationInfo principle = (SimpleAuthenticationInfo) userAuthenticationInfo;
final SimplePrincipalCollection simple = (SimplePrincipalCollection) principle.getPrincipals();
authorizationCache.remove(simple);
}
}

最佳答案

如果这个类是您唯一使用缓存的地方,您可以保留对缓存的最终引用。一遍又一遍地调用缓存管理器和缓存是没有用的。

如果您知道缓存管理器在此时已初始化,我建议您在构造函数中执行此操作。

关于java - 我怎样才能在应用程序的任何部分从 shiro 框架中获取 cacheManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33254863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com