How To Set Up Redis as a Cache for MySQL

  • Tuesday, 26 September 2023
  • PHP
  • 1 Comment/s

Redis (Remote Dictionary Server) is a fast open-source, in-memory database that you can use as a key-value store for a highly scalable and performance-oriented system. Some of Redis’ use cases include: caching, high-speed transactions, real-time analytics, live notifications, machine learning, searching, and queue/job processing. Since Redis is an in-memory key-value store, its performance makes it suitable for caching data in your application.

$redis6-cli monitor
sudo nano /var/www/html/products.php
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$key = 'PRODUCTS';
...
if (!$redis->get($key)) { $source = 'MySQL Server'; $database_name = 'test_store'; $database_user = 'test_user'; $database_password = 'PASSWORD'; $mysql_host = 'localhost'; $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 products"; $stmt = $pdo->prepare($sql); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $products[] = $row; } $redis->set($key, serialize($products)); $redis->expire($key, 10);
} else { $source = 'Redis Server'; $products = unserialize($redis->get($key));
}
echo $source . ': <br>';
print_r($products);
?>
foreach($products as $result) {	$blogImg = $result['blogImg'];	}

To delete keys from all Redis databases, use the following command:

redis6-cli flushall

Full article link

 


Related Tags

redislinuxphp

Share Post
  • Jacob
    05 October 2023 @ 15:36

    To delete keys from all Redis databases, use the following command: redis-cli flushall

PHP

How to Generate Captcha Image in PHP ?

14-09-2023
PHP

How To Set Up Redis as a Cache for MySQL

26-09-2023
PHP

Submit a form without page refresh

08-12-2022