gpt4 book ai didi

java - CacheLoader 多次加载相同的键

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:55 24 4
gpt4 key购买 nike

我的 cacheIterator 中有重复的键。

我正在调用一个使用 SOAP 的 Web 服务来为保险公司的保单评级。我正在尝试使用 Cachebuilder/loader 将 DTO 存储为键并将服务的响应存储为值。根据我的研究,.get 和 .getUnchecked 方法将从缓存中获取一个值,如果它不存在,它将将该值加载到缓存中。这是一些代码:

public class CacheLoaderImpl
{
private static CacheLoaderImpl instance = null;
private static LoadingCache<PolicyDTO, RatingServiceObjectsResponse> responses;

protected CacheLoaderImpl()
{
responses = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(
new CacheLoader<PolicyDTO, RatingServiceObjectsResponse>() {
public RatingServiceObjectsResponse load(PolicyDTO key)
throws Exception
{
return getResponse(key);
}
});
}

public static CacheLoaderImpl getIntance()
{
if(instance == null)
{
instance = new CacheLoaderImpl();
}

return instance;
}

public LoadingCache<PolicyDTO, RatingServiceObjectsResponse> getResponses()
{
return responses;
}

public RatingServiceObjectsResponse getResponse(PolicyDTO key) throws ExecutionException
{
RatingServiceObjectsResponse response = new RatingServiceObjectsResponse();
try
{
response = new CGIRatabaseServiceImpl().getCoverages(key);
}
catch (RemoteException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
return response;

}

}

这就是我调用 get 方法的地方:

RatingServiceObjectsResponse response = CacheLoaderImpl.getIntance().getResponses().get(policy.toCoveragesCallDTO()); 

我假设它可能正在比较不同的内存地址,所以我重写了 toString 方法以将 DTO 对象转换为 JSON。检查缓存后,我可以看到键与比较工具完全相同。然而,它们仍然被存储并每次都调用该服务。我尝试覆盖 PolicyDTO 上的 equals 方法,但在调试时它从未命中。

我怎样才能让缓存加载器只加载不同键的值,并按照最初的意图提取现有值?

我想我只是不太清楚 cacheLoader 的实际工作原理。我感谢任何帮助或建议。

PolicyDTO 类:

public class PolicyDTO extends AbstractDto implements IPolicyDTO
{
private ArrayList<ILineOfBusinessDTO> lobDTOs = new ArrayList<ILineOfBusinessDTO>();
private String pcInd;
private String ratingEffectiveDate;
private String companyName;

public String getPcInd()
{
return pcInd;
}

public void setPcInd(String pcInd)
{
this.pcInd = pcInd;
}

public String getRatingEffectiveDate()
{
return ratingEffectiveDate;
}

public void setRatingEffectiveDate(AdvancedDate ratingEffectiveDate)
{
if(ratingEffectiveDate != null)
{
this.ratingEffectiveDate = ratingEffectiveDate.toFormattedStringMMDDYYYY();
}
else
{
this.ratingEffectiveDate = new AdvancedDate().toFormattedStringMMDDYYYY();
}
}

public String getCompanyName()
{
return companyName;
}

public void setCompanyName(String companyName)
{
this.companyName = companyName;
}

public DtoType getType()
{
return hasGetCoveragesCoverageDTO() ? DtoType.GET_COVERAGE_POLICY : DtoType.RATE_POLICY;
}

public boolean hasGetCoveragesCoverageDTO()
{
if(lobDTOs != null)
{
for(ILineOfBusinessDTO lineDTO : lobDTOs)
{
if(lineDTO.hasGetCoveragesCoverageDTO())
{
return true;
}
}
}

return false;
}

@Override
public void addLob(ILineOfBusinessDTO lob) {

lobDTOs.add(lob);
}

@Override
public Iterator<ILineOfBusinessDTO> getLobIterator() {

return lobDTOs.iterator();
}

public ICoverageDTO findCoverage(String coverageID)
{
ICoverageDTO coverageDTO = null;

for(ILineOfBusinessDTO lineDTO : lobDTOs)
{
coverageDTO = lineDTO.findCoverage(coverageID);

if(coverageDTO != null)
{
return coverageDTO;
}
}

return null;
}
@Override
public String toString()
{
return JSONConversionUtility.convertPolicyDTO(this);
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result
+ ((companyName == null) ? 0 : companyName.hashCode());
result = prime * result + ((lobDTOs == null) ? 0 : lobDTOs.hashCode());
result = prime * result + ((pcInd == null) ? 0 : pcInd.hashCode());
result = prime
* result
+ ((ratingEffectiveDate == null) ? 0 : ratingEffectiveDate
.hashCode());
return result;
}

@Override
public boolean equals(Object object)
{
if(object instanceof PolicyDTO)
{
return object.toString().equals(this.toString());
}
return false;

}
}

最佳答案

您的 PolicyDTO 类的 hashCodeequals 不一致 - 它违反了以下规则:

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

缓存使用 hashCode(很像 HashMap 类),所以当它看到两个具有不同哈希码的键时,它会假定它们不相等。

关于java - CacheLoader 多次加载相同的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31125109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com