Redis synchronizes with MySQL data

Source: Internet
Author: User
Tags mysql update

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.