* evict : 驅逐,趕出
ps : 使用shiro進行權限管理後,每次都須要調用realm查詢角色和權限,每次都須要查數據庫,性能不是很好java
pps : 是否能夠將數據庫中的數據放到緩存中,減小數據庫交互,提升性能?web
<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
複製src/main/resources
更名ehcache.xml
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache>
<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>
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManager"/> </bean> <cache:annotation-driven cache-manager="springCacheManager"/>
@Override @CacheEvict(value="myCache",allEntries=true) public void save(User user) { userDao.save(user); }
@Override @Cacheable("myCache") public List<User> findAll() { return userDao.findAll(); }
key
,支持對象嵌套,支持spel表達式@Override @Cacheable(value="myCache",key="#pageable.pageNumber+'_'+#pageable.pageSize") public List<User> findPageData(Pageable pageable) { return userDao.findAll(pageable); }