spring 2.5 annotations配置ehcache

來源:互聯網
上載者:User

配置ehcache地址:

http://code.google.com/p/ehcache-spring-annotations/wiki/UsingTriggersRemove

hi :

大家好,我目前使用的是spring ehcache作為項目的緩衝.詳細配置如下:

ehcaceh_config.xml

<?xml version="1.0" encoding="utf-8"?>

<beans default-autowire="byName"

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

       http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring

       http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven cache-manager="ehCacheManager" />

<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

<property name="configLocation" value="/WEB-INF/classes/ehcache.xml"></property>

</bean>

</beans>

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">

<diskStore path="java.io.tmpdir/EhCacheSpringAnnotationsExampleApp"/>

    <defaultCache

            maxElementsInMemory="100"

            eternal="false"

            timeToIdleSeconds="3000"

            timeToLiveSeconds="3000"

            overflowToDisk="true"

            diskSpoolBufferSizeMB="30"

            maxElementsOnDisk="100"

            diskPersistent="false"

            diskExpiryThreadIntervalSeconds="100"

            memoryStoreEvictionPolicy="LRU"

            statistics="false"

            />

    <cache name="UserCache"

           maxElementsInMemory="100"

           maxElementsOnDisk="100"

           eternal="false"

           overflowToDisk="true"

           diskSpoolBufferSizeMB="20"

           timeToIdleSeconds="100"

           timeToLiveSeconds="100"

           memoryStoreEvictionPolicy="LFU"

           transactionalMode="off"

            />

</ehcache>

@Cacheable(cacheName="userCache")

public ConcurrentMap<Integer, UserModel> getAllUserInfoMap() {

List<UserModel> list = (List<UserModel>) getSqlMapClientTemplate()

.queryForList("user.SELECT_USER_ALL_INFO");

ConcurrentMap<String, UserModel> userMap = null;

if (CollectionUtils.isNotEmpty(list)) {

userMap = new ConcurrentHashMap<String, UserModel>();

for (UserModel userModel : list) {

userMap.put(userModel.getId(), userModel);

}

}

return userMap;

}

@TriggersRemove(cacheName="userCache",removeAll=true)

public void insertUser(UserModel userModel){

getSqlMapClientTemplate().insert("user.INSERT_USER_POJO",  userModel);

}

在這裡用的同學要注意你緩衝的密度

我建議大家用下面的放方式

    @Cacheable(cacheName="cacheName",keyGenerator = @KeyGenerator (
            name = "HashCodeCacheKeyGenerator",
            properties = @Property( name="includeMethod", value="false" )
        )
    )

有興趣的同學可以看一下ehcache source 它的緩衝是通過hashcode 來控制

具體可以看AbstractCacheKeyGenerator、、

{code}

public final T generateKey(MethodInvocation methodInvocation) {
        final Object[] arguments = methodInvocation.getArguments();
       
        if (this.includeMethod) {
            final Method method = methodInvocation.getMethod();
           
            final Class<?> declaringClass = method.getDeclaringClass();
            final String name = method.getName();
            final Class<?> returnType = method.getReturnType();

            if (this.includeParameterTypes) {
                final Class<?>[] parameterTypes = method.getParameterTypes();
                return this.generateKey(declaringClass, name, returnType, parameterTypes, arguments);
            }
           
            return this.generateKey(declaringClass, name, returnType, arguments);
        }
       
        try {
            return this.generateKey(arguments);
        }
        finally {
            if (this.checkforCycles) {
                //Cleanup our thread local data
                REGISTRY.remove();
            }
        }
    }

{code}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.