Using Redis to read and write data, while using queue processor timing to write data to MySQL, the problem is mainly how to ensure that MySQL and Redis data synchronization,
The key of the data synchronization is the primary key in MySQL database, the scheme is to go to MySQL to read all the table key values in Redis when it is started, and to write data to Redis, the Redis master
Key to self-increment and read, if the MySQL update fails, you need to clear the cache and synchronize the Redis primary key in time.
1String Tbname ="Login";2 3 //get MySQL Table primary key value--redis when started4 LongID =Mysql.getid (tbname);5 //set the Redis primary key value--redis when started6Redisservice.Set(Tbname, string.valueof (ID));7 8System. out. println (ID);9 LongL =redisservice.incr (tbname);TenSystem. out. println (l); OneLogin Login =NewLogin (); A Login.setid (l); -Login.setname ("Redis"); - Redisservice.hmset (string.valueof (Login.getid ()), login); the -Boolean B = Mysql.insert ("INSERT INTO login (id,name) VALUES ("+ Login.getid () +", '"+ login.getname () +"')"); - /** - * + * Queue processor update MySQL failed: - * + * Clear Cache data While primary key value is self-reducing A */ at if(!b) { -REDISSERVICE.DELKEYANDDECR (Tbname,"Login:"+string.valueof (Login.getid ())); - } -System. out. println (Redisservice.exists ("Login:"+string.valueof (Login.getid () )); -System. out. println (Redisservice.Get(Tbname)); -Copy Code
Redis synchronizes with MySQL data