使用shiro,在ShiroConfig中定义实例对象securityManager:
@Bean
public SecurityManager securityManager(MyShiroRealm myShiroRealm, RedissonWebSessionManager redissonWebSessionManager, RedissonShiroCacheManager redissonShiroCacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 自定义缓存实现 使用redis
securityManager.setCacheManager(redissonShiroCacheManager);
return securityManager;
}
其中redissonShiroCacheManager就是自定义的redis实现缓存,redissonShiroCacheManager中获取cache对象getCache。主要介绍cache对象存取数据的过程:
RedissonShiroCache<K, V> implements Cache<K, V>
@Override
public V get(K key) throws CacheException {
Object value = (RedissonShiroCache)this.map.get(key); //获取缓存
return fromStoreValue(value);
}
this.map.get(key)获取缓存,其中map的类型是RMap<K, Object> map,当调用get()方法时,会调用RMap的实现类RedissonMap<K, V>:
public V get(Object key) {
return this.get(this.getAsync(key));
}
下面是put方法,同上面的get方式流程:
RedissonShiroCache<K, V> implements Cache<K, V>
@Override
public V put(K key, V value) throws CacheException {
Object previous = this.map.put(key, val, config.getTTL(), TimeUnit.MILLISECONDS,
config.getMaxIdleTime(), TimeUnit.MILLISECONDS);
return fromStoreValue(previous);
}
RedissonMap<K, V>
public V put(K key, V value) {
return this.get(this.putAsync(key, value));
}