You have to implement your own CacheManager and Cache and configure Shiro to
use them. These are my classes for Infinispan; you can use them as examples
for your Redis implementation:

shiro.ini
[main]
...
securityManager.cacheManager = $cacheManagerInfinispan
...

-------------------------------------------------------------------------
package de.scsynergy.elementary.qi.shiro;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.apache.shiro.util.Destroyable;
import org.infinispan.manager.EmbeddedCacheManager;
import org.ops4j.pax.shiro.cdi.ShiroIni;

@ShiroIni
public class CacheManagerInfinispan implements CacheManager, Destroyable {

    @Resource(lookup = "java:jboss/infinispan/container/web")
    private EmbeddedCacheManager cacheContainer;
    private static final Pattern PATTERN =
Pattern.compile("([^.]+)\\.authorizationCache\\.?\\d*");
    private final Map<String, Cache> mapCache = new HashMap();

    protected CacheManagerInfinispan() {
    }

    @PostConstruct
    public void init() {
    }

    @PreDestroy
    @Override
    public void destroy() {
        for (String name : cacheContainer.getCacheNames()) {
            if (!CachingSessionDAO.ACTIVE_SESSION_CACHE_NAME.equals(name)) {
                mapCache.get(name).clear();
            }
        }
        mapCache.clear();
    }

    @Override
    public <K, V> Cache<K, V> getCache(String name) throws CacheException {
        Cache cache = mapCache.get(name);
        if (cache == null) {
            Matcher matcher = PATTERN.matcher(name);
            if (matcher.find()) {
                name = matcher.group(1);
            }
            cache = new CacheInfinispan(cacheContainer.getCache(name,
"persistent"));
            mapCache.put(name, cache);
        }
        return cache;
    }
}

-------------------------------------------------------------------------
package de.scsynergy.elementary.qi.shiro;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.shiro.cache.CacheException;
import org.infinispan.Cache;

public class CacheInfinispan<K, V> implements
org.apache.shiro.cache.Cache<K, V> {

    private Cache<K, V> cache;

    protected CacheInfinispan() {
    }

    public CacheInfinispan(Cache cache) {
        this.cache = cache;
    }

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

    @Override
    public V put(K key, V value) throws CacheException {
        return cache.put(key, value, 1, TimeUnit.DAYS);
    }

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

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

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

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

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




--
Sent from: http://shiro-user.582556.n2.nabble.com/

Reply via email to