将ehcache与spring 3.0集成

问题描述 投票:2回答:3

我有一个应用程序,我使用spring 3.0.2和ibatis。现在,我需要将ehcache与我的代码集成。我试过this link,但无法让它工作。我希望有人给我详细说明所需的罐子,xml配置和代码更改(如果需要)。

java spring caching ehcache
3个回答
2
投票

升级到最新的春季3.1里程碑 - 它通过注释内置缓存支持 - see here

除此之外,你总是可以使用EhCacheFactoryBean


1
投票

要在您的应用程序中实现此,请按照下列步骤操作

步骤1:

将罐子添加到Ehcache Annotations for Spring project site上列出的应用程序中。

第2步:

将注释添加到要缓存的方法。让我们假设您正在使用上面的Dog getDog(String name)方法:

@Cacheable(name="getDog")
Dog getDog(String name)
{
    ....
}

第3步:

配置Spring。您必须在beans声明部分中将以下内容添加到Spring配置文件中:

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

有关完整的详细信息,请参阅Ehcache site


0
投票

要集成Ehcache,请按照以下步骤操作

1 - 在pom XML文件中添加依赖项

<dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache-core</artifactId>
   <version>2.6.9</version>
</dependency>

2 - 创建一个名为spring-cache.xml的xml文件,将其放在resources文件夹中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd">

    <cache:annotation-driven/>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache" />
    </bean>

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>

</beans>

3 - 正如您所看到的,我们正在使用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="true"
    monitoring="autodetect" dynamicConfig="true">
    <cache name="users" maxEntriesLocalHeap="5000"
        maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20"
        timeToIdleSeconds="200" timeToLiveSeconds="500"
        memoryStoreEvictionPolicy="LFU" transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>

因此可以看到为“用户”创建缓存,以便可以在从数据库查询用户列表的任何位置使用

4 - 像下面的代码一样使用它

@Cacheable(value="users")
public List<User> userList() {
    return userDao.findAll();
}

这就像你可以在任何需要的地方实现缓存一样

仍然有一些疑问或困惑看现场演示

Integrate EhCache in Spring MVC

© www.soinside.com 2019 - 2024. All rights reserved.