Get redis value using PHP

Sekarang kita akan mencoba mengambil value dari key “skills39:index” yang terdapat pada redis-server.

Install dulu paket web server nya, jangan lupa install juga paket ekstensi redis-server

# apt-get install apache2 php5 php5-redis curl -y

pertama, kita uji coba dulu php nya, buat file index.php di directory web (“/var/www/html/”) .

# nano /var/www/html/index.php

Kemudian isi dengan syntax untuk menguji php :

<?php

echo “Testing php n”;

?>

save file tersebut, kemudian akses file php menggunakan curl.

# curl http://localhost/index.php
Testing php

Jika muncul tulisan “Testing php” artinya php sudah berjalan dengan baik. Kemudian aktifkan modul php redis menggunakan perintah :

# php5enmod redis

Restart apache agar php terestart :

# /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.

Sekarang buat directory untuk menampung file php redis nantinya.

# mkdir /var/www/html/redis

buat file php nya.

# nano /var/www/html/redis/index.php

isi dengan syntax dibawah ini :

<?php
$redis = new Redis();
$redis->connect(“<server>“);
$content = $redis->get(“<key>“);
echo $content;
?>

Keterangan :

1. ganti “<server>” dengan ip address dari redis server.

2. ganti “<key>” dengan key redis yang nantinya kita ambil value nya.

Sehingga menjadi :

<?php
$redis = new Redis();
$redis->connect(“192.168.58.2”);
$content = $redis->get(“skills39:index”);
echo $content;
?>

Save file tersebut, kemudian uji menggunakan curl :

# curl http://localhost/redis/
Today is the [Sun Dec 30 14:59:01 WIB 2018] and in one hour is [Sun Dec 30 15:59:01 WIB 2018]

Pastikan muncul value yang mirip dengan saat menjalankan perintah :

# redis-cli get skills39:index
"Today is the [Sun Dec 30 15:01:01 WIB 2018] and in one hour is [Sun Dec 30 16:01:01 WIB 2018]"

Terimakasih


Tinggalkan komentar