如何安全的更新java本地缓存
1个回答
展开全部
java安全的更新本地缓存的方式如下:当外部请求访问缓存数据时:如果缓存已经过期(当前时间-缓存的上次更新时间超过缓存的有效期),则重新调用webservice访问服务端查询数据,然后更新缓存。如果缓存未过期,但缓存为空,则重新调用webservice访问服务端查询数据,然后更新缓存。<pre t="code" l="java">/** 本地缓存 */
private List<InterfaceConfig> configs = null;
/** 本地缓存的上次更新时间 */
private long lastUpdateTime = 0;
public List<InterfaceConfig> queryInterfaceList() {
long currentTime = System.currentTimeMillis();
//判断本次缓存是否过期,过期则重新调用webservice查询数据,并更新缓存
if (currentTime - lastUpdateTime > 60000) {
InterfaceManageResult result = interfaceManageFacade.queryAllInterfaceList();
if (null != result result.isSuccess()) {
configs = result.getInterfaceConfigList();
}
lastUpdateTime = currentTime;
}
if (!CollectionUtils.isEmpty(configs)) {
return configs;
}
//本地缓存为空,则重新调用webservice查询数据,并更新缓存
InterfaceManageResult result = interfaceManageFacade.queryAllInterfaceList();
if (null == result || !result.isSuccess()) {
return null;
}
configs = result.getInterfaceConfigList();
return configs;
}
private List<InterfaceConfig> configs = null;
/** 本地缓存的上次更新时间 */
private long lastUpdateTime = 0;
public List<InterfaceConfig> queryInterfaceList() {
long currentTime = System.currentTimeMillis();
//判断本次缓存是否过期,过期则重新调用webservice查询数据,并更新缓存
if (currentTime - lastUpdateTime > 60000) {
InterfaceManageResult result = interfaceManageFacade.queryAllInterfaceList();
if (null != result result.isSuccess()) {
configs = result.getInterfaceConfigList();
}
lastUpdateTime = currentTime;
}
if (!CollectionUtils.isEmpty(configs)) {
return configs;
}
//本地缓存为空,则重新调用webservice查询数据,并更新缓存
InterfaceManageResult result = interfaceManageFacade.queryAllInterfaceList();
if (null == result || !result.isSuccess()) {
return null;
}
configs = result.getInterfaceConfigList();
return configs;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询