redis和mysql数据同步_请教redis如何做到和mysql数据库的同步

参考代码如下:

String tbname = "login";

//获取mysql表主键值--redis启动时

long id = MySQL.getID(tbname);

//设置redis主键值--redis启动时

redisService.set(tbname, String.valueOf(id));

System.out.println(id);

long l = redisService.incr(tbname);

System.out.println(l);

Login login = new Login();

login.setId(l);

login.setName("redis");

redisService.hmset(String.valueOf(login.getId()), login);

boolean b = MySQL.insert("insert into login(id,name) values(" + login.getId()

+ ",'" + login.getName() + "')");

/**

*

* 队列处理器更新mysql失败:

*

* 清除缓存数据,同时主键值自减

*/

if (!b)

{

redisService.delKeyAndDecr

(tbname, "Login:"+String.valueOf(login.getId()));

// redisService.delete("Login:"+String.valueOf(login.getId()));

//redisService.decr(tbname);

}

System.out.println(redisService.exists("Login:"+String.valueOf(login.getId())));

System.out.println(redisService.get(tbname));

你可能感兴趣的:(redis和mysql数据同步)