gpt4 book ai didi

java - 如何在创建新条目时更新 java spring 框架中的缓存

转载 作者:行者123 更新时间:2023-11-29 04:41:37 25 4
gpt4 key购买 nike

创建用户函数:

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

获取用户函数:

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

请帮助我如何在创建新用户时更新缓存,以便当我从 List<Users> getOnlineUsers() 获取用户时jquery json 函数的功能我应该得到更新的缓存。谢谢

最佳答案

如果你想使用@CacheEvict 清除整个缓存那么你可以在注解参数中使用allEntries = true

@CacheEvict(value="defaultCache",  allEntries = true) 
public boolean createUser(Users user, HttpSession session) { ....

关于java - 如何在创建新条目时更新 java spring 框架中的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38981270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com