【发布时间】: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 服务的输出。
【问题讨论】: