从上文:Shiro+springboot+mybatis(md5+salt+散列)认证与授权-02
当每次进行刷新时,都会从数据库重新查询数据进行授权操作,这样无疑给数据库造成很大的压力,所以需要引入缓存机制,解决频繁访问数据库的压力
使用Shiro默认的EhCache缓存进行优化
1.导入依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.7.0</version>
</dependency>
2.在拦截器中开启缓存机制
customerRealm.setCacheManager(new EhCacheManager());
customerRealm.setCachingEnabled(true);
customerRealm.setAuthenticationCachingEnabled(true);
customerRealm.setAuthenticationCacheName("authenticationCache");
customerRealm.setAuthorizationCachingEnabled(true);
customerRealm.setAuthorizationCacheName("authorizationCache");
测试:
清空日志信息
再次刷新
说明是从缓存中读取的
每次执行都会先从缓存中查看是否有相应的数据,如果有,直接读取出来即可,如果没有,从数据库读取,然后一并放到缓存中