Shiro+springboot+mybatis+EhCache(md5+salt+散列)认证与授权-03
从上文: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");

测试:



清空日志信息
再次刷新

说明是从缓存中读取的
每次执行都会先从缓存中查看是否有相应的数据,如果有,直接读取出来即可,如果没有,从数据库读取,然后一并放到缓存中
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
