Laravel 6 redis caching not able to get cached items - caching

[Update]
I find that laravel cashes in only 1 key no matter how many keys I try to save but I always find only 1 key this one "laravel:9a261d789b8265a6b0495ea5d87481b4589422b1"
I have tried
if (Cache::store('redis')->has('bar')) {
echo ' - bar key cache exists - ';
}
else{
echo ' - bar key cache does not exist - ';
}
Cache::store('redis')->put('bar', 'baz', 600);
$value = Cache::get('bar');
echo $value; exit;
And I always get - bar key cache does not exist - baz why!!??
I'm trying to implement redis caching in Laravel Homestead localhost here's the .env
PP_NAME=mysite
APP_ENV=local
APP_KEY=base64:887AJ9PHxl7W/QS4g8VrMch6CTL4QMLneq4dHx2KhoE=
APP_DEBUG=true
APP_URL=https://mysite.test:44300
PAGINATE=10
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=redis
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
and config/database.php
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
in config/cache.php
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
Now in the controller:
$posts = Cache::remember('posts_cache', 600, function () use ($where) {
return Post::where($where)->inRandomOrder()->paginate(3);
});
when I print $posts every time I get different posts not the cached 3 posts. I run
redis-cli monitor
And find that it's caching. I run
redis-cli
Then
keys *
I get this
1) "laravel:9a261d789b8265a6b0495ea5d87481b4589422b1"
2) "laravel:9a261d789b8265a6b0495ea5d87481b4589422b1:timer"
How can I retrieve cached data from redis? what's wrong with my code?

Related

cakephp3 redis not configured properly

I am having a super hard time getting redis to be configured. It was working, but I upgraded my servers and composer updated- now I getting "redis is not configured correctly"
define('_CACHE_SERVER', '*********.cache.amazonaws.com:6379');
'_cake_core_' => [
'className' => 'Redis',
'servers'=> [ _CACHE_SERVER ],
'prefix' => 'mrg_cake_core_',
'serialize' => 'php',
'duration' => '+2 minutes',
],
I have written a blank php and connected to redis with just simple $redis = new Redis(); and got $redis->lastSave(); Seems like something changed in CAKE. Very confused.
Thank you ndm, I changed the config to:
define('_CACHE_SERVER', '************.cache.amazonaws.com');
'_cake_core_' => [
'className' => 'Redis',
'host' => _CACHE_SERVER,
'port' => 6379,
'prefix' => 'myapp_cake_core_',
'duration' => '+1 years',
'url' => env('CACHE_CAKECORE_URL', null),
],

What will be the expiry time, when we set SESSION_DRIVER / CACHE_DRIVER as redis in Laravel?

Session lifetime is set as below
'lifetime' => 10080
Now I just want to set an expiry time when we set "SESSION_DRIVER" as Redis
SESSION_DRIVER=redis
If we set "CACHE_DRIVER" as Redis cache instead of file what will expire time or how can we set expire time for "CACHE_DRIVER"
CACHE_DRIVER=redis
To solve this problem we need to tell Laravel there are different Redis connections, one for Cache and one for Session.
First, navigate to config/database.php at the bottom of the file you'll see the settings for Redis and there will be a default connection. We need to add a new connection, using the same host, password and host but change the database number.
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
Now we need to tell redis to use this connection as the session. Open up the config/session.php file and find the connection property, then change this to 'session'.
'connection' => 'session',

Why MaxAttemptsExceededException

I don't think I understand Laravel's queue system.
I implemented Redis (basic configuration) and I am doing very basic testing, but this error MaxAttemptsExceededExceptionis causing me a lot of confusion.
The idea of this "test" would be to simply run the controller via the browser, and as the controller runs, it "dispatches" the job, inside the for loop. In this test, I aim to send 1 email every 25 seconds, I imagine that each time I run the controller, the emails are "stacked".
Initially everything runs, but suddenly, php artisan queue:work reports error.
The point is:
What is the reason for this timeout? Is it a delay of Redis?
Is this parameter correct to use queue:work --tries=3?
Are there any settings I should change?
.ENV
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
SESSION_CONNECTION=default
CONFIG / DATABASE.PHP
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
CONFIG / SESSION.PHP
'connection' => env('SESSION_CONNECTION', null),
MY CONTROLLER
for ($i = 1; $i <= 5; $i++)
{
$when = now()->addSeconds(25 * $i);
$mykey = 'KEY--' . $i . '--' . $when->toDateTimeString();
dispatch(new TestRedisJob($mykey));
}
MY JOB CLASS (TestRedisJob)
public function handle(Redis $redis)
{
Redis::throttle('email')->allow(1)->every(25)->then(function ()
{
\Mail::to('customer#domain.com')
->send(new OrderShipped('contato#fiscalizo.com.br', $this->mykey));
}, function() {
return $this->release(25);
});
MY MAILABLE CLASS (OrderShipped)
public function build()
{
return $this
->from($this->emailFrom)
->subject("Registro Efetivado: " . $this->emailSubject)
->view('emails.welcome');

Configure Redis Sentinel in Laravel 5.5

How to configure the Redis sentinel? Redis can be configured in stand alone easily using laravel configs but when using sentinel how to configure is not documented anywhere?
There is one similar question asked on redit: but no help.
In Laravel 5.5 one can do it like this:
Reference: https://github.com/laravel/framework/pull/18850#issue-116339448
database.php:
'redis' => [
'client' => 'predis',
// Keep Default as is you want to use both redis and sentinel for different service(cache, queue)'
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
// Create a custom connection to use redis sentinel
'cache_sentinel' => [
// Set the Sentinel Host from Environment (optinal you can hardcode if want to use in prod only)
env('CACHE_REDIS_SENTINEL_1'),
env('CACHE_REDIS_SENTINEL_2'),
env('CACHE_REDIS_SENTINEL_3'),
'options' => [
'replication' => 'sentinel',
'service' => 'cachemaster'),
'parameters' => [
'password' => env('REDIS_PASSWORD', null),
'database' => 0,
],
],
],
],
```
Specify the redis connection in your service where you want to use. example if cache needs redis sentinal can create new cache connection to use the above sentinal connection like this:
'stores' = [
//Keep default too as is
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
// create own cache connection
'sentinel_redis' => [
'driver' => 'redis',
'connection' => 'cache_sentinel',
],
And in Laravel app you can easily use via Cache Facade:
Cache::store('sentinel_redis')->get('key');

No connector for []

I am using redis as my Queue_driver but when I try to run
php artisan queue:listen vvv
it says
[InvalidArgumentException]
No connector for []
I have redis set up in my queue.php
'default' => env('QUEUE_DRIVER', 'redis'),
'connections' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'retry_after' => 90,
],
],
and set the queuedriver in my .env file
CACHE_DRIVER=redis
QUEUE_DRIVER=redis
composer.json
"require":{
"predis/predis":"~1.0",
Maybe worth to mention that I'm using docker to run my project and redis is working, I use it for caching and it works as expected.
Hope you guys can help me.
You need to specify the connection details in config/database.php (or specify the ENV vars):
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
This can be found right at the end of the file. (This one is taken from Laravel 5.4 and might be different depending on your version of Laravel).

Resources