Configure Memcached in Laravel - 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

Related

Laravel memcached not working in shared hosting

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.

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.

Memcache not working with Laravel but it's working when using it directly

I have memcached set up on my server.
If I use the following code -
$meminstance = new Memcached();
$meminstance->addServer("127.0.0.1", 11211);
$meminstance->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$meminstance->setSaslAuthData("user", "pass");
$result = $meminstance->get("test");
if ($result) {
echo $result;
} else {
echo "No matching key found. Refresh the browser to add it!";
$meminstance->set("test", "Successfully retrieved the data!") or die("Couldn't save anything to memcached...");
}
then everything works perfectly.
However, if I set the same settings via laravel then the cache does not work at all. When I check debug bar, the cache keeps missing.
.env
MEMCACHED_USERNAME=user
MEMCACHED_PASSWORD=pass
MEMCACHED_HOST=127.0.0.1
MEMCACHED_PORT=11211
config/cache.php
'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID', 1),
'sasl' => [
env('MEMCACHED_USERNAME'),
env('MEMCACHED_PASSWORD'),
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT => 2000,
//Memcached::OPT_BINARY_PROTOCOL => true,
],
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],
What can be the cause of this? I've noticed that if I enter the incorrect credentials or server details, no error appears. Is it possible that Laravel just isn't communicating with the memcached server?
I've managed to fix it by enabling UDP in memcached.
sudo nano /etc/memcached.conf
and remove the line
-U 0

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 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.

Resources