【问题标题】:I am unable to store data using ehcache我无法使用 ehcache 存储数据
【发布时间】:2018-06-25 13:43:17
【问题描述】:

我是缓存世界的新手,我正在尝试实现缓存。我想缓存为某些数据调用 Web 服务的方法的输出,以避免为相同的数据一次又一次地调用 Web 服务。但是我的缓存不起作用,我的方法是一次又一次地调用 Web 服务来获取相同的数据。

spring-server.xml

   <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"/>
<property named = "shared" value="true"/>
</bean>

ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="ehcache.xsd"
   updateCheck="true"
   monitoring="autodetect"
   dynamicConfig="true">

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

<cache name="loadstatus"
maxEntriesLocalHeap = "10000"
maxEntriesLocalDisk= "100000"
eternal = "false"
diskSpoolBufferSizeMB = "20"
timeToIdleSeconds="28800" timeToLiveSeconds="28800"
memoryStoreEvictionpolicy ="LFU"`enter code here`
transactionalMode ="off">
<persistance strategy = "localTempSwap"/>
</cache>       
</ehcache>

服务等级

@Service
public class ServiceImplementation {

@Autowired
ClientImplementation webServiceClient;

@Cacheable(value = "loadstatus")    
public List<GetLoadStatusResonseElement> getLoadStatus() throws Exception{
List<GetLoadStatusResonseElement> list = null;
list = webServiceClient.getLoadStatus();
return list;
}
}

PS:来自 web 服务的数据进入我的方法 loadstatus 需要每 24 小时刷新一次。但是同一天的数据将保持不变,因此对于特定的一天,我想缓存 Web 服务的输出。

【问题讨论】:

    标签: spring caching ehcache


    【解决方案1】:

    仅指定@Cacheable 是不够的。您还需要告诉 Spring 应该查找这些注释。

    添加&lt;cache:annotation-driven/&gt; 应该可以解决它(当然,您需要添加对应的xsd)

    【讨论】:

    • 我已经在我的代码中提到过,但在这里忘记了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多