0

I have implement redis cache concept in my core php (custom application) db is Mysql. php with mysql redis cache concept its working fine.

In my Mysql table i have 20 records I tried to exceute php code its get data from db and stored cache its working fine. suppose I have updated or delete data from mysql table but its not changing in redis cache. Redis cache its shows only old 20 records there (with out new changes). mysql table changes is not changing in redis cache. How to solve it?

my php code for the reference

 <?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$key = 'PRODUCTS';
if (!$redis->get($key)) {
$source = 'MySQL Server';
$database_name     = 'xxxx';
$database_user     = 'yyyy';
$database_password = 'password';
$mysql_host        = 'ip_address';
$pdo = new PDO('mysql:host=' . $mysql_host . '; dbname=' . $database_name, $database_user, $database_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql  = "SELECT * FROM table_name";
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$products[] = $row;
}
$redis->set($key, serialize($products));
$redis->expire($key, 10000);
} else {
$source = 'Redis Server';
$products = unserialize($redis->get($key));
}
$keys = array_keys($products);
echo $source . ': <br>';
print_r($products);
?>
<br />
<table class="table1 table-hover fullwidth">
<thead>
<tr>
<th class="text-md-center">S.NO.</th>
<th>INVOICE NO</th>
<th>FULL NAME</th>
<th>EMAIL</th>
<th>PHONE</th>                                                
<th>PURCHASED VIA</th>
<th>STATUS</th>
<th>REG DATE</th>
<th>EXP DATE</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<?php
for($i=0; $i < count($keys); ++$i) {
?>
<tr class="row_div_<?php echo $products[$keys[$i]]['war_reg_pk']; ?>">
<td class="text-md-center"><?php echo $keys[$i]; ?></td>
<td><?php echo $products[$keys[$i]]['war_inv_number']; ?></td>
<td><?php echo $products[$keys[$i]]['war_fullname']; ?></td>
<td><?php echo $products[$keys[$i]]['war_email']; ?></td>
<td><?php echo $products[$keys[$i]]['war_phone']; ?></td>
<td><?php echo $products[$keys[$i]]['war_purchased_via']; ?></td>
<td><?php echo $products[$keys[$i]]['war_status']; ?></td>
<td><?php echo $products[$keys[$i]]['war_reg_date']; ?></td>
<td><?php echo $products[$keys[$i]]['war_exp_date']; ?></td>
<td class="text-center"><a href="edit_sold_item_details.php?edit_pk=<?php echo $war_reg_pk;?>"><span class="btn btn-outline-danger"><i class="fa fa-pencil-square"></i></span></a></td>
<td class="text-center"><span class="btn btn-outline-danger remove_user_account btn-sm" id="<?php echo $war_reg_pk; ?>"><i class="fa fa-trash"></i></span></td>
</tr>
<?php
}
?>
</table>
Udhayakumar
  • 261
  • 1
  • 10
  • clear the redis cache after you insert/update/delete from the tables. – danblack Jan 10 '22 at 04:29
  • hi @danblack this is the only way we can solve this issue or any other better solution is available in the industry? – Udhayakumar Jan 10 '22 at 09:36
  • If you want to have the cache update with DB changes - there are CDC(Change Data Capture) solutions built around Debezium. Or if you want the DB to receive cache changes - there are Redis Gears write behind solutions – namizaru Jan 13 '22 at 19:28
  • hi @namizaru I m very new to CDC and Debezium concept, can you give me the sample code for CDC using Debezium? based on my above code i already configured correctly php,mysql and redis combination its working. now i m looking sample code for CDC using Debezium as well – Udhayakumar Jan 20 '22 at 03:29
  • https://github.com/tgrall/redis-microservices-demo includes some examples – namizaru Jan 28 '22 at 19:56

0 Answers0