Thursday, September 2, 2021

.::: How To Set Up Redis as a Cache for MariaDB / MySQL with PHP :::.


1. Install Redish

2. Install lamp

[root@teguhth /]# yum -y install php libapache2-mod-php php-pdo php-json php-cli

3. Install MariaDB

4. Create Database MariaDB, Table & row 

4.1 Create User MariaDB 
MariaDB [(none)]> create user 'teguh' identified by 'triharto';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all privileges on teguhdb.* to 'teguh'@'%' identified by 'triharto' with grant option;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

4.2 Create Database & table 

MariaDB [(none)]> use teguhdb;
MariaDB [(none)]> create table barang(KODE_BARANG char(6) not null, NAMA_BARANG varchar(25), SATUAN_BARANG varchar(20), STOK_BARANG decimal(4), primary key (KODE_BARANG));
MariaDB [(none)]> 

4.3 Create row data 
MariaDB [(none)]> insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-01','RICE COOKER','BUAH',20);
MariaDB [(none)]> insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-02','LEMARI ES','UNIT',8);
MariaDB [(none)]> insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-03','TELEVISI','UNIT',30);
MariaDB [(none)]> insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-04','RADIO/TAPE','BUAH',35);
MariaDB [(none)]> insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-05','KOMPUTER','UNIT',28);
MariaDB [(none)]> insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-06','KIPAS ANGIN','BUAH',38);
MariaDB [(none)]> select * from barang;

5. Create a PHP Application to Fetch Data from MariaDB/MySQL

[root@teguhth redis]# cat /var/www/html/barang.php
<html>
  <head>
    <title>Using Redis Server with PHP and MariaDB/MySQL</title>
  </head> 
  <body>

    <h1 align = 'center'>Barang' Register</h1>

    <table align = 'center' border = '2'>        

    <?php 
        try {

            $data_source = '';

            $redis = new Redis(); 
            $redis->connect('127.0.0.1', 6379); 

            $sql = 'select kode_barang, nama_barang, satuan_barang, stok_barang from barang';

            $cache_key = md5($sql);

            if ($redis->exists($cache_key)) {

                $data_source = "Data from Redis Server";
                $data = unserialize($redis->get($cache_key));

            } else {

                $data_source = 'Data from MariaDB/MySQL Database';

                $db_name     = 'teguhdb';
                $db_user     = 'teguh';
                $db_password = 'triharto';
                $db_host     = 'localhost';

                $pdo = new PDO('mysql:host=' . $db_host . '; dbname=' . $db_name, $db_user, $db_password);
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $stmt = $pdo->prepare($sql);
                $stmt->execute();
                $data = []; 

                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {          
                   $data[] = $row;  
                }  

                $redis->set($cache_key, serialize($data)); 
                $redis->expire($cache_key, 10);        
           }

           echo "<tr><td colspan = '4' align = 'center'><h2>$data_source</h2></td></tr>";
           echo "<tr><th>Kode Barang</th><th>Nama Barang</th><th>Satuan Barang</th><th>Stok Barang</th></tr>";

           foreach ($data as $record) {
              echo '<tr>';
              echo '<td>' . $record['kode_barang'] . '</td>';
              echo '<td>' . $record['nama_barang'] . '</td>';
              echo '<td>' . $record['satuan_barang']  . '</td>';     
              echo '<td>' . $record['stok_barang']  . '</td>';   
              echo '</tr>'; 
           }              

        } catch (PDOException $e) {
            echo 'Database error. ' . $e->getMessage();
        }
   ?>

    </table>
  </body>
</html>
[root@teguhth redis]# 


6. Testing data from MariaDB MariaDB & Redis



No comments:

Post a Comment

Popular Posts