Reputation: 1073
How can i check the availability of Redis connection in Laravel 5.4. I tried below code, but getting an exception with ping line. How can i do, if Redis is not connected than do something else to avoid exception?
No connection could be made because the target machine actively refused it. [tcp://127.0.0.1:6379]
use Redis;
class SocketController extends Controller
{
public function sendMessage(){
$redis = Redis::connection();
if($redis->ping()){
print_r("expression");
}
else{
print_r("expression2");
}
}
}
Also tried this:
$redis = Redis::connection();
try{
$redis->ping();
} catch (Exception $e){
$e->getMessage();
}
But unable to catch exception
Upvotes: 7
Views: 34489
Reputation: 376
I usually set a key in Redis from PHP side, then from the redis-cli
, I fetch the key to make sure that everything is working fine.
You can use Tinker to put the key.
use Illuminate\Support\Facades\Redis;
Redis::set('k,'OK');
$ redis-cli
$ get k
"OK"
Upvotes: 0
Reputation: 184
This files is routes/console.php and here I tested my "hello world" for Redis server it worked fine. I created an artisan command.
php artisan vibhore
will do all the redis related experiment here.
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Redis;
//use Illuminate\Support\Facades\Cache;
Artisan::command('vibhore', function () {
//$this->comment(Inspiring::quote());
echo "vibhore is saying hello world by making his own command";//i got success in this.
//Redis::set('vj_key', 'vj_value');
//Redis::del('vj_key');
//Redis::expire('key', 30 * 60);//setting and expire time to redis.
//Cache::put('vj_key', 'vj_value', 30);//it will be alive for only 30 seconds.
//echo Cache::get('vj_key');
//echo Redis::get('vj_key');
})->purpose('Display an vibhore quote');
Later on I learned more about where to use Redis (in middleware) that in routes/web.php, while defining the routes, we can use them as
Route::get('/test-redis', function () {
// Store value in cache
//Cache::put('vj_key2', 'vj_value2', 30);
// Retrieve value from cache
//$cacheValue = Cache::get('vj_key2');
// Directly get value from Redis
//Redis::set('vj_key2','vj_setvalue');
//$redisValue = Redis::get('vj_key2');
//Redis::del('vj_key2');
//Redis::expire('key', 30 * 60);
return "Cache value: $cacheValue, Redis value: $redisValue";
});
Upvotes: 1
Reputation: 865
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class ConnectionChecker
{
public static function isDatabaseReady($connection = null)
{
$isReady = true;
try {
DB::connection($connection)->getPdo();
} catch (\Exception $e) {
$isReady = false;
}
return $isReady;
}
public static function isRedisReady($connection = null)
{
$isReady = true;
try {
$redis = Redis::connection($connection);
$redis->connect();
$redis->disconnect();
} catch (\Exception $e) {
$isReady = false;
}
return $isReady;
}
}
I created a helper class to check redis and db connection
Upvotes: 9
Reputation: 578
In your constructor you can check redis status connection like this:
/**
* RedisDirectGeoProximity constructor.
* @throws RedisConnectionException
*/
public function __construct()
{
try
{
$this->redisConnection = Redis::connection('default');
}
catch ( Exception $e )
{
throw new RedisConnectionException([
'message' => trans('messages.redis.fail'),
'code' => ApiErrorCodes::REDIS_CONNECTION_FAIL
]);
}
}
and in your exception :
namespace App\Exceptions\Redis;
/**
* Class RedisConnectionException
* @package App\Exceptions\Redis
*/
class RedisConnectionException extends \Exception
{
/**
* RedisConnectionException constructor.
* @param array $options
*/
public function __construct(array $options = [])
{
parent::__construct($options);
}
}
Upvotes: 2
Reputation: 13669
if you are using predis , then it will throw Predis\Connection\ConnectionException
Error while reading line from the server. [tcp://127.0.0.1:6379] <--when redis is not connected
so catch that Exception, you will get your redis is connected or not .
use Illuminate\Support\Facades\Redis;
public function redis_test(Request $request){
try{
$redis=Redis::connect('127.0.0.1',3306);
return response('redis working');
}catch(\Predis\Connection\ConnectionException $e){
return response('error connection redis');
}
Upvotes: 12
Reputation: 15464
Make sure redis is installed
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install redis-server
To test that your service is functioning correctly, connect to the Redis server with the command-line client:
redis-cli
In the prompt that follows, test connectivity by typing:
ping
You should see:
Output
PONG
Install Laravel dependencies
composer require predis/predis
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04
https://askubuntu.com/questions/868848/how-to-install-redis-on-ubuntu-16-04
Upvotes: 2