John Davidson

php - Redis deleting key when it should set it

0 comments
Message:


Hey all I have this following code in a Laravel API:


if (Hash::check($token_query, $hashed_token))
{
Auth::loginUsingId($key->id);
$getSessionID = session()->getId($key->id);
$cookie = cookie('session', $getSessionID, 15);
self::updateIntoDatabase('users', 'identifier', $identifier_query, 'active_session', $getSessionID);
Cache::put('Session_Token: ' . $getSessionID, true,900);
return response(["status" => 200, "message" => "Logged in", "loginUsingId:" => Auth::user()], 200)->withCookie($cookie);
}

When using keys * after sending this request, nothing appears. However, using monitor outputs this:


1646681598.414829 [0 172.21.0.4:60140] "SELECT" "0"


1646681598.415269 [0 172.21.0.4:60140] "DEL" "laravel_database_laravel_cache:9C5AGmh99tVzG9Sc0OTWygfh1pXvFEnCg1Ckr4nB"


It appears to be selecting the 0th database - which is correct - and then deleting the key?


Here's my config/database.php:


'redis' => [

'client' => env('REDIS_CLIENT', 'phpredis'),

'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],

'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],

'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', 'laravel'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],

],

My config/cache.php:


'redis' => [
'driver' => 'redis',
'connection' => 'cache',
'lock_connection' => 'default',
],

My .env:


BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=redis
REDIS_PASSWORD=laravel
REDIS_PORT=6379


My Answer:
It looks like the issue might be with the configuration of your Redis cache. In your `config/cache.php` file, you are specifying the `lock_connection` as `default`, but in your `config/database.php` file, you are not defining a connection named `default`.

Try updating your `config/cache.php` file to match the connection name in your `config/database.php` file. For example:

php
'redis' => [
'driver' => 'redis',
'connection' => 'default', // Change this to 'default'
'lock_connection' => 'default',
],


Make sure that the connection name matches the one defined in your `config/database.php` file. This should ensure that the cache is being set correctly instead of being deleted.

Rate this post

5 of 5 based on 2231 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us