ajax获取shiroehcache,Shiro – 使用EhCache缓存
在Shiro中加入緩存可以使權(quán)限相關(guān)操作盡可能快,避免頻繁訪問數(shù)據(jù)庫獲取權(quán)限信息,因?yàn)閷τ谝粋€(gè)用戶來說,其權(quán)限在短時(shí)間內(nèi)基本是不會變化的。
在上一篇Shiro – 權(quán)限控制中,當(dāng)用戶訪問”獲取用戶信息”、”新增用戶”和”刪除用戶”的時(shí)候,后臺輸出了三次打印信息
這對數(shù)據(jù)庫來說是沒必要的消耗。接下來使用緩存來解決這個(gè)問題。
引入EhCache的相關(guān)依賴
org.apache.shiro
shiro-ehcache
1.3.2
org.springframework.boot
spring-boot-starter-cache
net.sf.ehcache
ehcache
org.apache.shiro
shiro-ehcache
1.3.2
org.springframework.boot
spring-boot-starter-cache
net.sf.ehcache
ehcache
添加EhCache的配置文件
在src/main/resource/..路徑下新增一個(gè)Ehcache配置——shiro-ehcache.xml:
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
name="passwordRetryCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true" />
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
name="passwordRetryCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true" />
將緩存對象注入到SecurityManager中
@Bean
public EhCacheManager getEhCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile("classpath:config/shiro-ehcache.xml");
return em;
}
@Bean
public SecurityManager securityManager(){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(shiroRealm());
securityManager.setRememberMeManager(rememberMeManager());
securityManager.setCacheManager(getEhCacheManager());
return securityManager;
}
@Bean
public EhCacheManager getEhCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile("classpath:config/shiro-ehcache.xml");
return em;
}
@Bean
public SecurityManager securityManager(){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(shiroRealm());
securityManager.setRememberMeManager(rememberMeManager());
securityManager.setCacheManager(getEhCacheManager());
return securityManager;
}
配置完畢啟動(dòng)項(xiàng)目,分別訪問訪問”獲取用戶信息”、”新增用戶”和”刪除用戶”,可發(fā)現(xiàn)后臺只打印一次獲取權(quán)限信息:
總結(jié)
以上是生活随笔為你收集整理的ajax获取shiroehcache,Shiro – 使用EhCache缓存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win7 Aero特效不显示怎么办(wi
- 下一篇: 服务器备份文件格式,证书服务器,备份,还