尝试通过Redis缓存检索数据时遇到以下错误:- 返回用于缓存操作的空键(也许您在没有调试信息的类上使用了命名的参数?)Builder [public java.util.List com.concretepage.CelebController.getAll()] caches = [celebcache] | key ='#p1'| keyGenerator =''| | cacheManager =''| | cacheResolver =''|条件=''|除非=''| sync ='false']的根本原因
已尝试通过将cachekey重命名为#p0但仍然面临相同的错误来解决其他类似类型的问题。
型号:-
import javax.persistence.*;
@Entity(name="celeb")
public class Celeb {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long id;
@Column(name="celeb_name")
private String name;
@Column(name="no_of_followers")
private long followers;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getFollowers() {
return followers;
}
public void setFollowers(long followers) {
this.followers = followers;
}
@Override
public String toString() {
return "Celeb{" +
"id=" + id +
", name='" + name + '\'' +
", followers=" + followers +
'}';
}
}
模型控制器:-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class CelebController {
Logger logger = LoggerFactory.getLogger("CelebController");
@Autowired
private CelebRepository celebRepository;
@PostMapping("/create")
public void createUser(@RequestBody Celeb user) {
celebRepository.save(user);
}
@Cacheable(value = "celebcache", key = "#p0")
@GetMapping("/getall")
public List<Celeb> getAll() {
logger.info("Returning from DB");
return celebRepository.findAll();
}
@PostMapping("/delete")
public void createUser(@RequestParam(value = "userid")Integer id) {
celebRepository.deleteById(id);
}
}
答案 0 :(得分:0)
正如您所指出的,尝试#p0
和#p1
无效。这是因为它将方法 parameters 引用到了方法...在您的情况下,其中没有一个!参见:
// #p0 and #p1 reference non-existing method parameters...
@Cacheable(value = "celebcache", key = "#p0")
public List<Celeb> getAll(/* zero input parameters */) { ... }
对于每个the docs here,您完全不必指定key
:
如果未提供任何参数,则返回SimpleKey.EMPTY
因此以下内容就足够了:
@Cacheable(value = "celebcache")