2016-07-19 3 views

답변

0

shiro에서 캐싱을 구현하려면 org.apache.shiro.cache.Cache 및 org.apache.shiro.cache.CacheManager를 구현해야합니다. 당신이 infinispan를 사용하려면 다음 단계를 수행 :

이 org.apache.shiro.cache.Cache이

import java.util.Collection; 
import java.util.Set; 

import org.apache.shiro.cache.Cache; 
import org.apache.shiro.cache.CacheException; 


public class InfinispanCache<K, V> implements Cache<K, V> { 


    private final org.infinispan.Cache<K, V> cacheProxy; 

    public InfinispanCache(final org.infinispan.Cache<K, V> cacheProxy) { 
     this.cacheProxy = cacheProxy; 
    } 


    @Override 
    public V get(final K key) throws CacheException { 
     return cacheProxy.get(key); 
    } 


    @Override 
    public V put(final K key, final V value) throws CacheException { 
     return cacheProxy.put(key, value); 
    } 


    @Override 
    public V remove(final K key) throws CacheException { 
     return cacheProxy.remove(key); 
    } 


    @Override 
    public void clear() throws CacheException { 
     cacheProxy.clear(); 
    } 


    @Override 
    public int size() { 
     return cacheProxy.size(); 
    } 


    @Override 
    public Set<K> keys() { 
     return cacheProxy.keySet(); 
    } 


    @Override 
    public Collection<V> values() { 
     return cacheProxy.values(); 
    } 

} 

org.apache.shiro.cache.CacheManager

import org.apache.shiro.cache.Cache; 
import org.apache.shiro.cache.CacheException; 
import org.apache.shiro.cache.CacheManager; 
import org.infinispan.manager.CacheContainer; 


public class InfinispanCacheManager implements CacheManager { 


    private final CacheContainer cacheContainer; 

    public InfinispanCacheManager(final CacheContainer cacheContainer) { 
     this.cacheContainer = cacheContainer; 
    } 

    @Override 
    @SuppressWarnings({ "rawtypes", "unchecked" }) 
    public <K, V> Cache<K, V> getCache(final String name) throws CacheException { 
     return new InfinispanCache(cacheContainer.getCache(name)); 
    } 

} 

를 주입 구현 구현 캐시 컨테이너. 렐름이 CDI가 가능한 경우 작동해야합니다.

import javax.annotation.Resource; 

/** The security cache manager. */ 
    @Resource(lookup = "java:jboss/infinispan/container/<YOUR CACHE CONTAINER NAME>") 
    private EmbeddedCacheManager securityCacheManager; 

은 영역 구현에서 캐시 매니저를 설정합니다

setCachingEnabled(true); 
setAuthenticationCachingEnabled(true); 
setAuthorizationCachingEnabled(true); 
setCacheManager(new InfinispanCacheManager(securityCacheManager)); 
setAuthenticationCacheName(authenticationCacheName); 
setAuthorizationCacheName(authorizationCacheName); 

을 마찬가지로, 다른 캐싱 frameworsk와 시로에서 구현할 수 있습니다.

관련 문제