本篇文章為大家展示了如何使用Shiro性能優(yōu)化EhCache,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
10年積累的網(wǎng)站設(shè)計、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有洱源免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
* evict : 驅(qū)逐,趕出
ps : 使用shiro進行權(quán)限管理后,每次都需要調(diào)用realm查詢角色和權(quán)限,每次都需要查數(shù)據(jù)庫,性能不是很好
pps : 是否可以將數(shù)據(jù)庫中的數(shù)據(jù)放到緩存中,減少數(shù)據(jù)庫交互,提高性能?
Shiro 默認對 ehcache 的支持
在后臺管理系統(tǒng)中 ehcache 使用非常普遍
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.8.RELEASE</version> </dependency>
解壓ehcache-core.jar
包 ,將ehcache-failsafe.xml
復(fù)制src/main/resources
改名ehcache.xml
默認緩存區(qū)
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache>
可以自定義緩存區(qū)(不想改的話照著默認的寫)
<cache name="myCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache>
<!-- spring整合ehcache --> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean>
<!-- shiro封裝ehCacheManager --> <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" > <property name="cacheManager" ref="ehCacheManager"/> </bean>
<!-- 配置subject的后臺推手securityManager --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> <property name="cacheManager" ref="shiroCacheManager"/> </bean>
<bean id="myRealm" class="club.info.bos.realm.MyRealm"> <property name="authorizationCacheName" value="myCache"/> </bean>
注意: 需要緩存的對象要實現(xiàn)serializable接口
spring提供一套整合緩存器的注解
開啟注解緩存
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManager"/> </bean> <cache:annotation-driven cache-manager="springCacheManager"/>
清除緩存,通常數(shù)據(jù)庫數(shù)據(jù)發(fā)生變化后,清除緩存,如增,刪改
@Override @CacheEvict(value="myCache",allEntries=true) public void save(User user) { userDao.save(user); }
能緩存的,查詢后緩存
@Override @Cacheable("myCache") public List<User> findAll() { return userDao.findAll(); }
針對數(shù)據(jù)在不同條件下進行不同緩存,我們可以指定緩存的key
,支持對象嵌套,支持spel表達式
@Override @Cacheable(value="myCache",key="#pageable.pageNumber+'_'+#pageable.pageSize") public List<User> findPageData(Pageable pageable) { return userDao.findAll(pageable); }
上述內(nèi)容就是如何使用Shiro性能優(yōu)化EhCache,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標題:如何使用Shiro性能優(yōu)化EhCache
地址分享:http://chinadenli.net/article40/geooho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站制作、商城網(wǎng)站、網(wǎng)站營銷、App開發(fā)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)