我正在Spring Boot中开发一个java应用程序。我使用Hazelcast进行缓存,但是当使用@CachePut注释方法时它不会更新缓存 我的代码是这样的:
@CachePut(cacheNames = "cachetest")
public void addData(com.nikesh.webapp.model.CacheTest cacheObj) {
repo.save(cacheObj);
}
@Cacheable(cacheNames = "cachetest")
public List<com.nikesh.webapp.model.CacheTest> getCacheTestData() {
return repo.findAll();
}
@SuppressWarnings("unchecked")
@Cacheable(cacheNames = "cachetest")
public List<com.nikesh.webapp.model.CacheTest> getCacheTestDataByPhone(
String phone) {
Query q = manager.createNamedQuery("byPhone");
q.setParameter("phone", phone);
return q.getResultList();
}
@CacheEvict(cacheNames = "cachetest")
public boolean clearCache() {
return true;
}
CacheEvict工作正常。我错过了什么吗?
答案 0 :(得分:0)
您没有在注释中指定缓存键。
您可以尝试以下方式:
@CachePut(cacheNames = "cachetest", key="#p0.phone")
public CacheTest addData(com.nikesh.webapp.model.CacheTest cacheObj) {
return repo.save(cacheObj);
}
@Cacheable(cacheNames = "cachetest", key="'phone'")
public List<com.nikesh.webapp.model.CacheTest> getCacheTestData() {
return repo.findAll();
}
@SuppressWarnings("unchecked")
@Cacheable(cacheNames = "cachetest", key="#p0")
public List<com.nikesh.webapp.model.CacheTest> getCacheTestDataByPhone(
String phone) {
Query q = manager.createNamedQuery("byPhone");
q.setParameter("phone", phone);
return q.getResultList();
}
@CacheEvict(cacheNames = "cachetest")
public boolean clearCache() {
return true;
}