У меня это работает, но Алекс прав, есть несколько разных способов настроить это, в зависимости от того, что вы хотите использовать в качестве бэкэнда кэширования.
Для настройки кэша я выбрал ehcache, так какего легко настроить, но он имеет мощные функции для настройки ttl / и т. д.
Конфигурация:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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 local="ehcache"/></property>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"/>
</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" monitoring="autodetect"
dynamicConfig="true">
<!-- http://ehcache.org/documentation/configuration.html -->
<!-- Also See org.springframework.cache.ehcache.EhCacheFactoryBean -->
<diskStore path="java.io.tmpdir"/>
<cache name="resourceBundle"
overflowToDisk="false"
eternal="false"
maxElementsInMemory="500"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"/>
</ehcache>
У меня были проблемы с запускомehcache 2.5 в моей среде junit из-за одновременного запуска кешей с дублирующимися именами не разрешался, и они не закрывались сразу, но вот моя запись в pom:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.4.7</version>
</dependency>
Наконец,в вашем хранилище выполните следующие действия:
@Repository
public class someStore {
@PersistenceContext
EntityManager em;
//The value here needs to match the name of the cache configured in your ehcache xml. You can also use Spel expressions in your key
@Cachable(value = "resourceBundle", key = "#basename+':'+#locale.toString()")
public ResourceBundle getResourceBundle(final String basename, final Locale locale){
...
}
@CacheEvict(value = "resourceBundle", key = "#basename+':'+#locale.toString()")
public void revertResourceBundle(final String basename, final Locale locale){
...
}
}