0

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
T. Duke
  • 646
  • 1
  • 6
  • 17

1 Answers1

0

There are a couple things to look at here:

  1. Your .env file has CACHE_DRIVER set to file, so your caching system is using the filesystem, not redis.

  2. If you do change CACHE_DRIVER to redis, you do not have the REDIS_CACHE_DB env variable set, so the caching system will use redis database 1 for caching. You will need to make sure to select database 1 when attempting to view the keys from the redis cli (see this answer for more information).

  3. The redis commands that you see happening are the garbage collection for your sessions, which are setup to use redis and database 0. The StartSession middleware has a lottery system built in, so that on every request to the application, it will randomly trigger the garbage collection to clean up expired sessions.

patricus
  • 59,488
  • 15
  • 143
  • 145