如何避免在带有spring的ehCache中指定所有缓存名称?

如何避免在带有spring的ehCache中指定所有缓存名称?,spring,ehcache,Spring,Ehcache,我正在我的应用程序中使用SpringCache,并希望实现ehCache。如何避免在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">

我正在我的应用程序中使用SpringCache,并希望实现ehCache。如何避免在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">    

   <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />           

   <cache name="test" maxElementsInMemory="10" eternal="true" overflowToDisk="false" />  

</ehcache>


我希望避免在XML中指定这里的所有缓存名称。有办法吗?

使用EhCache,您可以根据需要以编程方式创建所有缓存(例如,在应用程序启动时) 它仍然需要至少定义一个defaultCache的ehcache.xml…但是,您可以通过两种方式创建一个新的缓存:

1-基于ehcache.xml中定义的defaultCache:

   CacheManager singletonManager = CacheManager.create();
   singletonManager.addCache("testCache");
2-基于所需的自定义设置:

   //Create a singleton CacheManager using defaults
   CacheManager manager = CacheManager.create();

   //Create a Cache specifying its configuration.
   Cache testCache = new Cache(
   new CacheConfiguration("testCache", maxEntriesLocalHeap)
    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
    .eternal(false)
    .timeToLiveSeconds(60)
    .timeToIdleSeconds(30)
    .diskExpiryThreadIntervalSeconds(0)
    .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP)));

   //Remember to add the cache to the cacheManager: The cache is not usable until it has been added.
   manager.addCache(testCache);
更多信息请访问:和