简体   繁体   中英

how to keep the memcache in sync with mysql?

i would like to implement the web page using PHP & MySQL combination, it have an part for auto updating the count of notifications from DB, first i choose the ajax with set time interval to update the notifications count but when i going to the performance some suggestions said ajax is not ideal when many users on track. so i refer's the memcache mechanism, in my way it's looking good because it avoid the many request's hitting mysql continuously, so now the questoins is how keep the memcache in sync with mysql, i tried simple example with memcache,below mysq php code

<?php
 $meminstance = new Memcache();
 $meminstance->pconnect('localhost', 11211);

  mysql_connect("localhost", "appuser", "Appuser") or die(mysql_error());
  mysql_select_db("test") or die(mysql_error());

  $query = "select id,client,ip,count,title from mysql_common.alert_count";
  $querykey = "KEY" . md5($query);


  $result = $meminstance->get($querykey);

  if (!$result) {

    $query = mysql_query("select id,client,ip,count,title  from                   mysql_common.alert_count");
     while($row= mysql_fetch_array($query))
     {
    $id[]=$row['id'];
    $count[]=$row['count'];
    $title[]=$row['title'];
    $client[]=$row['client'];
    $ip[]=$row['ip'];
    }

    $arr['id']=$id;
    $arr['count']=$count;
    $arr['title']=$title;
    $arr['client']=$client;
    $arr['ip']=$ip;

    $result= json_encode($arr);

    $meminstance->set($querykey, $result, false, 20);

   echo $result;
   print "got result from mysql\n";
   return 0;
   }



  print_r($meminstance->get($querykey));

  print "got result from memcached\n";
 return 0;

  ?>

working flow of above code - first the memcache that having data for this query if then get from memcache else fetch result from mysql and store to memcache, but when i update the mysql table data and execute the same code it's giving result from memcache but that's is not updated data. please guide me to keep memcache in syncwith mysql. thanks

You need to invalidate data from cache when corresponding data in database is updated. One way may be to use unique id as key instead of whole query and whenever data for that id is updated, remove that item for cache. This way your application will reload entry from database.

However if you have range queries, it will be difficult to invalidate data from cache. Since you will never know whether an update will effect results of a cached query or not.

Caching solutions like NCache and Tayzgrid provide a feature of database dependency. Where a item in cache can be set dependent on a query on a database. Cache monitors database for changes and invalidates data from cache if corresponding results are updated. However such features are not available with memcache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM