Laravel redis works, but list still empty - laravel

i'm currently using redis in laravel everything works fine, unless i can't check my redis's keys like normally
I went to "redis-cli " and type "keys *" it shows -empty list or set-
But my cache worked. I tested it with laravel debugbar.
i'm not sure, where and how to check all my redis's keys via command line,
here is my config/database for redis
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', 'helper_'),
],
thanks
...

Assuming you are using default configuration, and have CACHE_DRIVER set as redis, then you are in fact writing to Redis. However you are by default writing to the database with index 1
You can see these entries by calling SELECT 1 followed by KEYS * in redis-cli.
You can specify which database to write to in a connection in config/database.php. You can then select which connection to use by default in config/cache.php.
This is why changing to default works in Nazmus Shakib's answer, because the default connection outlined in config/database.php uses database 0 rather than database 1. Alternatively, you could change REDIS_CACHE_DB to 0 in .env. Or, you could just use database 1.

Need to modify the file: config/cache.php
Change the stores > redis > connection
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
],
to
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
And make sure your .env file has CACHE_DRIVER=redis

Related

How to configure Laravel to use PhpRedis?

PHP 7.3
Laravel 5.8
Until now I was using Predis for my cache in the Laravel project. Now I want to switch to PhpRedis. I've read it's really simple (just config changes), but I have a lot of problems. I don't know what to begin with, so I'll write all what I know.
My hosting provider claims that PhpRedis is enabled.
The code below executed in a controller (Predis is set) works fine - I receive the set value.
$redis = new \Redis();
$redis->connect( 'socket path', 0 );
$redis->set('test', 'testValue');
print_r( $redis->get('test') );
However, the same code in the raw PHP file executed via SSH returns "Uncaught Error: Class 'Redis' not found in..."
Let's go to changes in config/database.php. Here is my configuration:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'/*'phpredis'*/),
'cluster' => true,
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'/*'redis'*/),
'prefix' => Str::slug(env('APP_NAME'), '_').'_',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
],
'default' => [
'scheme' => 'unix',
'path' => env('REDIS_HOST'),
'host' => env('REDIS_HOST'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT'),
'database' => env('REDIS_CACHE_DB', 0)
],
(...) // other
],
When I change values to these in comments, my website shows just a blank page - any errors in the mailbox.
Furthermore, when I run for example "php73 artisan config:clear" in the SSH, console returns "Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension." in the Illuminate/Redis/Connectors/PhpRedisConnector.php.
When I change the alias in config/app.php from "Redis" to "RedisManager" and try again it returns
Uncaught Error: Class 'Redis' not found in /path/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:70.
What's going on? How to set Laravel's configuration to use PhpRedis? Maybe it's my hosting provider issue? Thanks in advance for every advice.
If I missed some important code, give me a sign - I will add it.
The PHPRedis libraries are not installed by default in a shared hosting environment, and are generally not part of a PHP installation by default. You would have to ask your host to install these libraries within their shared hosting platform.

Why Laravel uses the default queue connection even if I specified a queue with different connection

I have problem with laravel queues. In my project, default connection is sync, I want to add sqs connection for one type of jobs.
When I dispatch job in this way:
TestAction::dispatch()->onQueue('test');
Job is performed immediately (by sync connection).
If I dispatch job in this way:
TestAction::dispatch()->onQueue('test')->onConnection('sqsTestAction');
everything is ok.
Queue "test" is in sqsTestAction connection, why in first example job is being sent by sync connection?
My config/queue.php:
'default' => env('QUEUE_CONNECTION', 'sync'),
'connections' => [
'sync' => [
'driver' => 'sync',
],
'sqsTestAction' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('AWS_SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => "test",
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
],
Laravel ver 5.8
why in first example job is being sent by sync connection?
Because it is set by default as you can see. Change .env file value for QUEUE_CONNECTION to sqsTestAction and that will become default.
In default key of config/queue file, second parameter is fallback parameter for case when .env value doesn't exist.

Why can't find my Laravel session keys in Redis container?

I have setup my Laravel application with docker, one container is dedicated to the app one for redis.
I have setup Laravel to use Redis for session an caching.
All works fine but if I enter my Redis container and try to list all keys like:
$redis-cli
#KEYS *
It will return only key values used for caching not the session keys.
The above is a doublecheck because actually from Laravel application I set session key and then dump like
<?php dump(session()->all()); dump(Session::getDefaultDriver()); ?>
and from the dump everything looks fine.
I see my session keys and values data structures.
Session::getDefaultDriver() //returns "redis"
So, by seeing Cache key:values inside redis container I assume that there's not a connection/docker container issues... Laravel is writing in the correct place. Redis default connection is shared by Cache and SEssion.
In database.php I have:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', 'redis'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
In session.php I have
'driver' => env('SESSION_DRIVER', 'redis'),
...
'lifetime' => env('SESSION_LIFETIME', 120),
By seeing the dumps returning correct values in Laravel web application I'm assuming session is working properly and points to redis.
What am I missing?

Session Management on different servers and differet domains with same database

I am building a site in Laravel 5, I need to manage session as something like this:
For Example. I have a site x#x.com hosting on server X and another y#y.com hosting on server Y along with database,(both server are of different countries) I need to use same database for the both site but session management is typical task for me in x#x.com as database is hosting on y#y.com. I am using Auth in laravel for authentication How that will be possible please help-.
You should use the same database connection on both servers. You can either make a small third server just for session management or you can simply tell X server to connect to the Y server database. You will first start by setting the environment variable SESSION_DRIVER or the configuration property session.driver to: database or redis depending on what you are using. Then create a connection the config file database.php under connections property if it's a RDBMS or under redis if it's a redis database.
'connections' => [
// ...
'session' => [
'driver' => 'mysql',
'host' => env('SESSION_DB_HOST'),
'database' => env('SESSION_DB_NAME'),
'username' => env('SESSION_DB_USERNAME'),
'password' => env('SESSION_DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]
// Or
'redis' => [
// ...
'session' => [
'host' => env('SESSION_REDIS_HOST'),
'port' => env('SESSION_REDIS_PORT', 6379),
'database' => 0,
],
]
Then in the file session.php, change the the value of connection to the name of the connection you want, session in this case.
Be mindful that you need to open the required ports and do some authorization process and take security measures on the host server (Y server since it has the database).
---- Answering the comment
Session driver is the mechanism that laravel uses to manage sessions. It can be on file, database, redis... So, when you choose the session driver in its config file and the driver is a database, you would specify a database connection for it or it will use the default database connection. So, if you create a third server and you want to use Redis as the database, you would open port 6379 (in case you're using the default redis configuration), then you create a connection in your Laravel installations. The connection in database.php will be like so:
'redis' => [
// ...
'my_session' => [
'host' => env('SESSION_REDIS_HOST'),
'port' => env('SESSION_REDIS_PORT', 6379),
'database' => 0,
],
]
Then in your environment or in the .env you add:
SESSION_REDIS_HOST=xx.xx.xx.xx // the ip address or domain of the third server
SESSION_REDIS_PORT= 6379
SESSION_DRIVER=redis
And finally, in you session.php config, you would set these values:
//...
'connection' => 'my_session',
You would do this on all Laravel installations that you want to be connected to the same session database.
Laravel provides database sessions which stores sessions in database so that you can use multiple servers for your application. Have a look at https://laravel.com/docs/5.3/session#database-sessions

Configure Memcached in Laravel

I've tried to set up memcached with my Laravel
Set Driver
/config/app.php
'default' => 'memcached',
Configure Memcache Server
'stores' => [
'database' => [
'driver' => 'database',
'table' => 'caches',
'connection' => null,
],
'memcached' => [
'driver' => 'memcached',
'servers' => [
[
'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
],
],
]
],
Cache
Route::get('cache/users', function() {
return Cache::remember('users', 60, function() {
return User::all();
});
});
How do I know I configure my caching with memcache properly ?
How do I see what I stored ?
First, you can use Cache::has('users') and Cache::get('users') in php artisan tinker or on a test route of some sort to see if stuff's being saved to the cache (and what the current contents are).
Second, you can connect to memcached (as well as other drivers like redis) via telnet and check directly.
telnet 127.0.0.1 11211
Once in, you can issue commands, like:
get users
which should spit out the contents of that cache key.
With the code you've shown, a non-working cache should also result in an exception. You can test this by turning off the memcached instance and refreshing the page - it should error out.
For the case when Memcache is on separate machine named memcached, i.e. docker with corresponding service: in .env set MEMCACHED_HOST=memcached

Resources