0

Getting below error while trying to retrieve data via redis cache:- Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public java.util.List com.concretepage.CelebController.getAll()] caches=[celebcache] | key='#p1' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'] with root cause

Have tried solution from other problems of similar types by renaming the cachekey to #p0 but still facing the same error.

Model:-

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 +
                '}';
    }
}

Model Controller:-

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);
    }


}

1 Answers1

0

As you pointed out, trying #p0 and #p1 didn't work. That's because it references the method parameters to the method... Of which there are none in your case! See:

// #p0 and #p1 reference non-existing method parameters...
@Cacheable(value = "celebcache", key = "#p0") 
public List<Celeb> getAll(/* zero input parameters */) { ... }

Per the docs here, you should just not have to specify the key at all:

If no params are given, return SimpleKey.EMPTY

So the following should suffice:

@Cacheable(value = "celebcache")
Dovmo
  • 8,121
  • 3
  • 30
  • 44
  • Yeah not getting the exception now but data is not getting cached. Where my data is actually getting stored in macbook? My current configuration is following:-@Cacheable(value = "celebcache",condition="#followers > 3000") @GetMapping("/getall") public List getAll() { logger.info("Returning from DB"); return celebRepository.findAll(); } but still all the time data is being fetched from DB. – Ravi Singh Shekhawat Jan 10 '19 at 16:50