Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update cache in java spring framework on creating new entry

Create user function:

@Autowired private org.springframework.cache.CacheManager cacheManager;
public boolean createUser(Users user,HttpSession session) {
    System.out.println("cache update changed");
    Cache c = cacheManager.getCache("defaultCache");
    if (c != null) {
        System.out.println("hello");
        c.put("",user.getUsername()); 
        // this code is not working 
    }

    //BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(user);

    MapSqlParameterSource params=new MapSqlParameterSource();
    params.addValue("username", user.getUsername());
    params.addValue("user_session_id", session.getId());
    java.util.Date today = new java.util.Date();
    //params.addValue("message_time",new java.sql.Timestamp(today.getTime()));

    params.addValue("user_last_activity_time", new java.sql.Timestamp(today.getTime()));

    jdbc.update("insert into users (username,user_session_id,user_last_activity_time) values (:username,:user_session_id,:user_last_activity_time)", params);// == 1;

    //c.clear();
    //return user;
    return true;
}

Get user function:

@Cacheable(value="defaultCache")
//@CachePut(value="defaultCache")
public List<Users> getOnlineUsers() {
    System.out.println("DB Query");
    return jdbc.query(sql, new RowMapper<Users>() {
        @Override
        public Users mapRow(ResultSet rs, int rowNumb) throws SQLException {
            Users user = new Users();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            return user;
        }
    });
}

please help me how can I update the cache on creating new user so when I get users from List<Users> getOnlineUsers() function by jquery json function i should get updated cache. Thanks

like image 415
dwivedy045 Avatar asked Oct 19 '25 04:10

dwivedy045


2 Answers

If you want to clear complete cache using @CacheEvict then you can use allEntries = true in annotation parameter

@CacheEvict(value="defaultCache",  allEntries = true) 
public boolean createUser(Users user, HttpSession session) { ....
like image 174
Bhupesh Avatar answered Oct 20 '25 18:10

Bhupesh


The exact way of doing this will depend on the cost of loading all users and the number of times users get created.

You basically have two options:

  • Invalidation based: Do not update the cache when creating a user, instead invalidate the mapping. Since you are using Spring, I recommend doing that through the Spring caching annotation @CacheEvict so that it happens outside of the transaction that created the user.
  • Manual: Keep going on the path you started, but there what you have to do is get the collection of users from the cache and add you new User to it. And then put that mapping back in the cache. For that you will need to figure out what Spring caching uses as default key, or specify a key in your @Cacheable annotation on getOnlineUsers.
like image 25
Louis Jacomet Avatar answered Oct 20 '25 18:10

Louis Jacomet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!