从上文: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");

在这里插入图片描述
测试:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

清空日志信息
再次刷新

在这里插入图片描述
说明是从缓存中读取的

每次执行都会先从缓存中查看是否有相应的数据,如果有,直接读取出来即可,如果没有,从数据库读取,然后一并放到缓存中

posted on 2020-12-30 17:00  凸凸大军的一员  阅读(58)  评论(0编辑  收藏  举报