Java 如何在应用程序的任何部分中从shiro框架获取cacheManager

Java 如何在应用程序的任何部分中从shiro框架获取cacheManager,java,shiro,Java,Shiro,如何在应用程序的任何部分中获取对Shiro框架中cacheManager对象的引用?例如,我想删除在删除用户或更新其权限期间缓存的旧用户数据。 现在我正在按照下面的方式处理它 public void cleanUserCache(final String userName) { final EmbeddedCacheManager embeddedCacheManager = securityRealmsProducer.getEmbeddedCacheManger();

如何在应用程序的任何部分中获取对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);
        }
    }
public void cleanUserCache(最终字符串用户名){
final EmbeddedCacheManager EmbeddedCacheManager=securityRealmsProducer.GetEmbeddedCacheManager();
final Cache authenticationCache=embeddedCacheManager.getCache(“JPA Auth Realm.authenticationCache”);
final Cache authrizationCache=embeddedCacheManager.getCache(“JPA Auth Realm.authorizationCache”);
最终对象userAuthenticationInfo=authenticationCache.get(用户名);
if(userAuthenticationInfo!=null){
authenticationCache.remove(用户名);
最终SimpleAuthenticationInfo原则=(SimpleAuthenticationInfo)用户身份验证信息;
最终SimplePrincipalCollection simple=(SimplePrincipalCollection)principle.getPrincipals();
authorizationCache.remove(简单);
}
}

如果该类是您使用缓存的唯一位置,则可以保留对缓存的最终引用。反复调用缓存管理器和缓存是没有用的


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

您所执行的操作有什么问题?还有,你看过单身模式吗?这个问题几乎就是它的目的!您必须小心使用单例模式。如果您懒散地进行初始化,则很容易出现并发问题。