当前位置 : 主页 > 编程语言 > c++ >

Spring RedisTemplate实现Shiro 的RedisCache 和 RedisSessionDao

来源:互联网 收集:自由互联 发布时间:2021-07-03
RedisTemplate 配置 RedisCache public class RedisCache implements Cache { private final Logger log = LoggerFactory.getLogger(getClass()); private String name; private RedisTemplate cache; public RedisCache(String name, RedisTemplate cache)
RedisTemplate 配置
 

 
        
  
        
  
        
   
    
  
        
   
    
  
        
   
    
  
        
   
    
  
    
 
    
 
    
 
        
  
        
  
        
   
    
  
        
   
    
  
        
   
    
  
        
   
    
  
    
 
RedisCache
public class RedisCache
 
   implements Cache
  
    { private final Logger log = LoggerFactory.getLogger(getClass()); private String name; private RedisTemplate
   
     cache; public RedisCache(String name, RedisTemplate
    
      cache) { this.name = name; this.cache = cache; } @Override public V get(K key) throws CacheException { if (log.isTraceEnabled()) { log.trace("Getting object from Redis HAHS K [" + name + "] for HK [" + key + "]"); } if (Objects.isNull(key)) { return null; } try { return cache.
     
      opsForHash().get(name, key); } catch (Throwable e) { throw new CacheException(e); } } @Override public V put(K key, V value) throws CacheException { try { V previous = get(key); cache.
      
       opsForHash().put(name, key, value); return previous; } catch (Throwable e) { throw new CacheException(e); } } @Override public V remove(K key) throws CacheException { try { V previous = get(key); cache.
       
        opsForHash().delete(name, key); return previous; } catch (Throwable e) { throw new CacheException(e); } } @Override public void clear() throws CacheException { try { cache.delete(name); } catch (Throwable e) { throw new CacheException(e); } } @Override public int size() { try { return cache.opsForHash().size(name).intValue(); } catch (Throwable e) { return 0; } } @Override public Set
        
          keys() { try { return cache.
         
          opsForHash().keys(name); } catch (Throwable e) { return Collections.emptySet(); } } @Override public Collection
          
            values() { try { return cache.
           
            opsForHash().values(name); } catch (Throwable e) { return Collections.emptyList(); } } }
           
          
         
        
       
      
     
    
   
  
 
RedisCacheManager
public class RedisCacheManager extends AbstractCacheManager {
    private RedisTemplate
 
   redisTemplate;
    private Map
  
    caches=new ConcurrentHashMap<>(); private String redisKeyPrefix="项目名:模块名:shiro:cache";//如 foo:backend:shiro:cache @Override protected Cache createCache(String name) throws CacheException { String cacheName=redisKeyPrefix+":"+name; RedisCache cache=caches.get(cacheName); if(Objects.isNull(cache)){ cache=new RedisCache(cacheName,redisTemplate); caches.put(cacheName,cache); } return cache; } public void setRedisKeyPrefix(String redisKeyPrefix) { this.redisKeyPrefix = redisKeyPrefix; } public void setRedisTemplate(RedisTemplate
   
     redisTemplate) { this.redisTemplate = redisTemplate; } }
   
  
 
RedisSessionDao
public class RedisSessionDao extends CachingSessionDAO {
  private static final String DEFAULT_REDIS_KEY_PREFIX = "项目名:模块名:shiro:ssession";//如 foo:backend:shiro:ssession
    private final Logger log = LoggerFactory.getLogger(RedisSessionDao.class);
    private RedisTemplate
 
   redisTemplate;
    private String redisKeyPrefix = DEFAULT_REDIS_KEY_PREFIX;

    @Override
    protected void doUpdate(Session session) {
        if (Objects.nonNull(session)) {
            log.trace("update session [{}]", session.getId());
            storeSession(session.getId(), session);
        }
    }

    @Override
    protected void doDelete(Session session) {
        if (Objects.nonNull(session)) {
            log.trace("delete session [{}]", session.getId());
            deleteSession(session.getId());
        }
    }

    @Override
    protected Serializable doCreate(Session session) {
        log.trace("create session...");
        Serializable sessionId = generateSessionId(session);
        assignSessionId(session, sessionId);
        storeSession(sessionId, session);
        log.trace("create session [{}] success", session.getId());
        return sessionId;
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        if (Objects.nonNull(sessionId)) {
            log.trace("read session [{}]", sessionId);
            return fetchSession(sessionId);
        }
        return null;
    }

    private void storeSession(Serializable sessionId, Session session) {
        redisTemplate.
  
   opsForHash().put(redisKeyPrefix, generateKey(sessionId), session); } private Session fetchSession(Serializable sessionId) { return redisTemplate.
   
    opsForHash().get(redisKeyPrefix, generateKey(sessionId)); } private void deleteSession(Serializable sessionId) { redisTemplate.opsForHash().delete(redisKeyPrefix, generateKey(sessionId)); } private String generateKey(Serializable sessionId) { return sessionId.toString(); } @Override public Collection
    
      getActiveSessions() { List
     
       sessions = redisTemplate.
      
       opsForHash().values(redisKeyPrefix); if(Objects.isNull(sessions)){ sessions=Collections.emptyList(); } log.trace("activeSessions [{}]", sessions.size()); return sessions; } public String getRedisKeyPrefix() { return redisKeyPrefix; } public void setRedisKeyPrefix(String redisKeyPrefix) { this.redisKeyPrefix = redisKeyPrefix; } public RedisTemplate
       
         getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate
        
          redisTemplate) { this.redisTemplate = redisTemplate; } }
        
       
      
     
    
   
  
 
网友评论