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>