Cara Menggunakan Redis di Laravel 5
![]() |
https://www.cloudways.com |
Cara Menggunakan Redis di Laravel 5
Apa itu Redis ?
Redis adalah project open source yang digunakan sebagai database, cache dan message broker. Redis mendukung struktur data seperti string, sets, hash, lists dan sorted sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Lebih lanjut tentang redis dapat dilihat di https://redis.io
Kebutuhan
Untuk kebutuhan artikel ini, saya asumsikan kamu sudah menginstall laravel di web server atau laptop kamu.Kebutuhannya ialah :
- Laravel 5.5
- PHP 7.1
- MySQL
Install Redis di server atau laptop.
Cara Menginstal Redis pada Ubuntu 18.04
Mempersiapkan Redis di Laravel
Sebelum menggunakan Redis di Laravel, kamu perlu menginstall paket predis/predis melalui Composer.
- Masuk ke root project lalu jalankan Composer Command, berikut perintahnya
composer require predis/predis
Konfigurasi
Konfigurasi redis diproject kamu berada di file config/database.php
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
Redis untuk Cache
- Edit file .env, ubah key menjadiCACHE_DRIVER = redis
- Berikut contoh menggunakan Cache UserController<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
{
/**
* Show a list of all users of the application.
*
* @return Response
*/
public function index()
{
$value = Cache::get('key');
//
}
}
- Mengambil value Dari Cache
$value = Cache::get('key');
$value = Cache::get('key', 'default');
- Check key pada Cacheif (Cache::has('key')) {
//
}
- Mengambil dan menyimpan cache$value = Cache::remember('users', $minutes, function () {
return DB::table('users')->get();
});
dengan perintah remember maka otomatis akan mengambil data jika value empty- Mengambil dan menghapus cache
$value = Cache::pull('key');
- Menyimpan value pada cache
Cache::put('key', 'value', $minutes);
Untuk menambahkan waktu kadaluarsa pada cache berikut contohnya :
$expiresAt = now()->addMinutes(10);
Cache::put('key', 'value', $expiresAt);
- Menyimpan value selamanya pada cache
Cache::forever('key', 'value');
- Menghapus cache
Cache::forget('key');
Redis untuk Session
Edit file .env, ubah key menjadi
SESSION_DRIVER=redis
Redis untuk QUEUE
Edit file .env, ubah key menjadi
QUEUE_DRIVER=redis
Untuk dukumentasi lengkapnya bisa dilihat di https://laravel.com/docs/5.5/cache
Cara Menggunakan Redis di Laravel 5 = redis, redis-laravel, laravel, laravel-shopping-cart, laravel-bootstrap, bootstrap, shopping-cart, shopping-cart-solution, e-commerce, e-commerceplatform, laravel-ecommerce, multi-language, payments, administration, php, laravel-cart, ecommerce, ecommerce-platform, ecommerce-website, ecommerce-shopping-solution, ecommerce-store, laravel5, free, source code. redis
Posting Komentar untuk "Cara Menggunakan Redis di Laravel 5"