Spring boot application with Redis cache and Mysql

Viewed 2305

I have been trying to develop a Spring boot application for caching using Redis cache. My application works for all methods except the update method. Here is the code I wrote

@RestController

@RequestMapping(value = "users/")

public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UserRepository userRepository;

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "add/", method = RequestMethod.POST)
    public void saveUser(@RequestBody User user){

        log.info("Saving User information to the database...");
        userService.saveUser(user);
    }

    @Cacheable(value = "user-get" , key = "#id")
    @RequestMapping(value = "get/{id}/", method = RequestMethod.GET)
    public User getUserById(@PathVariable Long id){

      return userService.getUserById(id);
    }

    @Cacheable(value = "user-get-all")
    @RequestMapping(value = "get/all/", method = RequestMethod.GET)
    public List<User> getAllUser(){

        return userService.getAllUsers();
    }

    @CacheEvict(value = "user-delete", key = "#id")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable Long id){

        userService.deleteUser(id);
    }

    @CachePut(value = "user-update", key = "#user.id")
    @RequestMapping(value = "update/{id}/", method = RequestMethod.POST)
    public User updateUser(@PathVariable Long id, @RequestBody User user){

        User newuser = new User();
        newuser = userRepository.findById(id).get();
        newuser.setUserName(user.getUserName());
        newuser.setAge(user.getAge());
        return userService.saveUser(newuser);
    }

}

Help me to fix the issue

2 Answers

If i remember correct, When using update, method must PUT or PATCH. Why you used POST.

@CachePut(value = "user-update", key = "#user.id")
@RequestMapping(value = "update/{id}/", method = RequestMethod.PUT)
public User updateUser(@PathVariable Long id, @RequestBody User user){

    User newuser = new User();
    newuser = userRepository.findById(id).get();
    newuser.setUserName(user.getUserName());
    newuser.setAge(user.getAge());
    return userService.saveUser(newuser);
}

I had a similar issue for me it turned out that when using CachePut on a method that has multiple input parameters (like id and user in your case). The default key generator then uses both arguments to generate a key. Even when you specify key = "#user.id" it fails to select that value. The fix is to use curly brackets around the key. In your case key = "{#user.id}". This is because if you had more than two arguments and wanted you use some of them for key generation you could specify them with comma separation inside the brackets like whenyou initialize an array "{#param1.foo2, #param4.bar1, #param6.foo1 }" etc.

Try this:

    @CachePut(value = "user-update", key = "{#user.id}")
    @RequestMapping(value = "update/{id}/", method = RequestMethod.POST)
    public User updateUser(@PathVariable Long id, @RequestBody User user){

    User newuser = new User();
    newuser = userRepository.findById(id).get();
    newuser.setUserName(user.getUserName());
    newuser.setAge(user.getAge());
    return userService.saveUser(newuser);
}
Related