Py学习  »  Redis

尝试使用spring boot在redis缓存中插入数据时,出现一些运行时错误

Ravi Singh Shekhawat • 4 年前 • 427 次点击  

尝试通过redis缓存检索数据时出现以下错误: 为缓存操作返回的空键(可能您正在没有调试信息的类上使用命名参数?)生成器[public java.util.list com.concretepage.celebcontroller.getall()]缓存=[celebcache]键='p1'键生成器=''''缓存管理器='''''缓存解析程序=''条件='''''124;除非='''''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);
    }


}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43677
 
427 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Dovmo
Reply   •   1 楼
Dovmo    5 年前

正如你所指出的,尝试 #p0 #p1 不起作用。因为它引用了 参数 方法…你的案子里没有!见:

// #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")