Laravel memcached not working in shared hosting - laravel

I've been trying to change the cache driver (CACHE_DRIVER) from file to memcached in production (shared host) where Memcached is set correctly and available according to phpinfo. Running still on Laravel 7.4.x
So far I've done the following:
On the .env file I've changed the CACHE_DRIVER=file to CACHE_DRIVER=memcached;
Checked the configuration in the config/cache.php as below:
<?PHP
'stores' => [
'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID'), // not required I think?...
'sasl' => [
env('MEMCACHED_USERNAME'), // not required I think?...
env('MEMCACHED_PASSWORD'), // not required I think?...
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT => 2000, // Ok
],
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'), // set like in phpinfo
'port' => env('MEMCACHED_PORT', 11211), // not required I think?...
'weight' => 100, // Ok
],
],
],
// ....
],
Ran php artisan config:clear and php artisan cache:clear;
Checked for existing cache tag. e.g. cache()->has('countries') and its false;
Changed back to CACHE_DRIVER=file and started over. Result for cache()->has('countries') and its true
Either probably I'm missing something in the process, yet in local it's running on Memcached and for performance improvement, I'm trying to change it in production.
Thanks in advance for any inputs that might help me to solve this issue.

I won't delete the question (answering here) as it may help others facing the same problem on shared hostings.
As #apokryfos mentioned the extension may be there but the Memcached server might not be available in all shared hostings, which is my case.
If you have your projects on shared hosting and want to use Memcached, contact their support and ask if your plan contemplates the Memcached server. Otherwise, use other cache systems, namely and ultimately the file cache.

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.

Laravel redis works, but list still empty

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

Laravel horizon: items no longer queued for no obvious reason

I've been running an app on a Laravel forged provisioned server.
We have some email jobs that are being queued, and we use Horizon to manage our queues. This has always worked without any issues, but for some reason, we have broken something, and I can't fix it.
This is our setup.
.env
APP_ENV=dev
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
QUEUE_DRIVER=redis
config/queues.php
return [
'default' => env('QUEUE_DRIVER', 'sync'),
'connections' => [
'sync' => [
'driver' => 'sync',
]
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'medium',
'retry_after' => 90,
],
],
];
config/horizon.php
return [
'use' => 'default',
'waits' => [
'redis:default' => 60,
],
'environments' => [
'dev' => [
'high-prio' => [
'connection' => 'redis',
'queue' => ['high'],
'balance' => 'simple',
'processes' => 10,
'tries' => 5,
],
'default-prio' => [
'connection' => 'redis',
'queue' => ['medium', 'low'],
'balance' => 'auto',
'processes' => 10,
'tries' => 3,
],
],
],
];
I checked the redis-cli info result to make sure the port was right:
forge#denja-dev:~$ redis-cli info
# Server
redis_version:3.2.8
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:11aa79fd2425bed9
redis_mode:standalone
os:Linux 4.4.0-142-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:5.4.0
process_id:1191
run_id:fcc57fa2c17440ab964538c2d986dc330d9e1223
tcp_port:6379
uptime_in_seconds:3045
uptime_in_days:0
hz:10
lru_clock:13667343
executable:/usr/bin/redis-server
config_file:/etc/redis/redis.conf
When I visit /horizon/dashboard, all is running fine.
I was playing a bit with adding some metadata to the payload for queued jobs, at which time the issues began. I then just removed that code again, and basically went back to the previous code base. There is no difference anymore, so I'm now suspecting that I have another issue.
However - I'm not getting ANY exception thrown when I add something to the queue. Bugsnag has no new entries, and my process just continues without any error.
Any idea what I can verify more to detect the actual issue? Is there a problem with the config? I'm a bit lost to be honest, especially since I have no information to work with :(
I also checked using tinker whether I could make a connection to redis, and that too works fine without an exception:
$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.0RC3 — cli) by Justin Hileman
>>> Illuminate\Support\Facades\Redis::connection('default')
=> Illuminate\Redis\Connections\PredisConnection {#3369}
The cause of this issue was that the notification that I was testing this with, did use the Queuable trait, but did not implement the ShouldQueue interface. The latter is required to have Laravel automatically queue these Notifications.
We noticed it when we started testing using other Notifications which went through fine.
The only question we still had is that we would have expected the email to go out nevertheless, since it would synchronously send it, which for some reason it did not.

Laravel 5 + memcached setup

I'm really just looking for an explanation about memecached and laravel. I understand what it does, but can I use my memcached installation with laravel. More specifically:
'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
'sasl' => [
env('MEMCACHED_USERNAME'),
env('MEMCACHED_PASSWORD'),
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT => 2000,
],
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],
I know/will set up the server aspect, and I get what the options do...but persistent_id, a memcached um and pw...what are they? Their uses? etc.. typically laravel is extremely well document but on memcached it says very little (And the little it does, seems to be dated and not based on 5.0 laravel)
Here is explanation from php.net:
By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.
http://php.net/manual/en/memcached.construct.php
So for your project just define a unique name for it.
Hope it helps.

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