RSS
热门关键字:
当前位置 :| 主页>java acegi>

Acegi 学习笔记(5) 使用User Cache

来源: 作者: 时间:2008-09-18 Tag: 点击:
如果考虑到效能的问题,当从资料库中撷取资料出来时,您会想到捞出来的资料是不是可以重复使用,不用每一次都连接资料库进行查询。

Acegi可以使用User Cache,当AuthenticationProvider需要使用者资料时,先行至User Cache中寻找是否有符合的项目,有的话就直接取回比对,没有的话,再从资料库或其它资料来源取得,这可以修改 第一个Acegi 程式 - 改用资料库作为验证来源 作为示范,只要修改一下daoAuthenticationProvider 的设定,并加入User Cache即可:
   ...
   <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> 
        <property name="userDetailsService" ref="jdbcDaoImpl"/>
        <property name="userCache" ref="userCache"/>
   </bean>
  
   <bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"> 
       <property name="dataSource" ref="dataSource"/>
   </bean> 
  
    <bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
        <property name="cache">
            <bean class="org.springframework.cache.ehcache.EhCacheFactoryBean">
                <property name="cacheManager">
                    <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
                </property>
                <property name="cacheName" value="userCache" />
            </bean>
        </property>
    </bean>
   ...

现在您可以重新启动程式,当查询到使用者资料时,会将之放入快取,之后若有相同的查询,会先至快取中查询有无资料。

由于使用了EhCache,所以别忘了加入ehcache-*.jar与相依的commons-collections-*.jar。
热点关注