@CacheEvict in Spring Boot is not deleting Cache in Redis

Viewed 1185

I am caching CRUD requests in Spring Boot using @Cacheable annotations. I Cache using Redis. The caching is done successfully. When I insert a new item or update an existing item I want to Evict or Delete several cache from Redis. For this I use the @CacheEvict tag in Spring Boot as below. However, deleting the cache is not happening. What is the problem here? (I am a beginner for Spring Environment and Redis)

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ExampleDB
spring.datasource.username=root
spring.datasource.password=#########
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15

spring.cache.type=redis
spring.redis.host=127.0.0.1
spring.redis.port=6379

spring.redis.lettuce.pool.max-active=7 
spring.redis.lettuce.pool.max-idle=7
spring.redis.lettuce.pool.min-idle=2

UserController.java

package com.springResearch.redisCaching;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.List;

@RestController(value = "/")
public class UserController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    private static final Logger logger = LogManager.getLogger("Log4J2AsyncLogger");


    @Caching(evict = { @CacheEvict(value = "getallusers"), @CacheEvict(value = "userbyid", key = "#id"), @CacheEvict(value = "getuserbyname")})
    @PostMapping(value = "/insertuser")
    public String insertUser(@RequestParam int id, @RequestParam String name, @RequestParam String address){
        jdbcTemplate.update("insert  into user(userId, username, address) values(?, ?, ?)", id, name, address);
        return "Success";
    }


    @Caching(evict = { @CacheEvict(value = "getallusers"), @CacheEvict(value = "getuserbyid", key = "#id"), @CacheEvict(value = "getuserbyname")})
    @PutMapping(value = "/updateuserbyid")
    public String updateUserByID(@RequestParam int id, @RequestParam String name){

        jdbcTemplate.update(
                "update user set username = ? where userId = ?", name, id
        );
        return "User succesfully updated";
    }


    @Cacheable(value = "getuserbyid", key = "#id")
    @GetMapping(value = "/getuserbyid")
    public List<Map<String, Object>> getUserById(@RequestParam int id){

        logger.info("Data queried from database");
        return this.jdbcTemplate.queryForList(
                "SELECT userId, username, address FROM user WHERE userId = ?", id

        );
    }


    @Cacheable(value = "getuserbyname")
    @GetMapping(value = "/getuserbyname")
    public List<Map<String, Object>> getUserByName(@RequestParam String name){

        logger.info("Data queried from database");
        return this.jdbcTemplate.queryForList(
                "SELECT userId, username, address FROM user WHERE username = ?", name

        );
    }


    @Cacheable(value = "getallusers")
    @GetMapping(value = "/getallusers")
    public List<Map<String, Object>> getAllUsers(){

        List<Map<String, Object>> result =  this.jdbcTemplate.queryForList(
                "SELECT * FROM user"
        );

        logger.info("Table user was queried from database");

        return result;
    }

}

Before calling updateUserByID cache in redis

127.0.0.1:6379> keys *

1) "getuserbyid::4"
2) "getuserbyname::sugan"
3) "getallusers::SimpleKey []"

After calling updateUserByID cache in redis

127.0.0.1:6379> keys *

1) "getuserbyid::4"
2) "getuserbyname::sugan"
3) "getallusers::SimpleKey []"

I want to delete the above cache records when calling insertUser or updateUserByID

0 Answers
Related