R
R
Ruslan2021-12-20 15:10:12
Yii
Ruslan, 2021-12-20 15:10:12

How to get session key for Redis in YII2?

I've been trying to get the correct session key for an hour, tell me where I'm missing something?
There is a config:

'session' => [
            'class' => 'yii\redis\Session',
            'redis' => 'redis',
            'keyPrefix' => 'QAINAR_SESS_',
            'timeout' => 3600,
        ],


echo "<pre>";
echo Yii::$app->session->id . PHP_EOL;
print_r(Yii::$app->session->keyPrefix . md5(json_encode([__CLASS__, Yii::$app->session->id])));
echo  PHP_EOL;
print_r(Yii::$app->redis->executeCommand('KEYS QAINAR_SESS_*'));
echo "</pre>";

I get the following:
8878353c8ca97c34021d62279cc7959a
QAINAR_SESS_85584472a3d4ad2bec0984e356079817
Array
(
    [0] => QAINAR_SESS_fd4011b394ac47f32666d77e2ecd7e6d
    [1] => QAINAR_SESS_e8192112a517f5f3b0e0e56def0aa2ce
)

As you can see, there is no QAINAR_SESS_85584472a3d4ad2bec0984e356079817 key in REDIS...
Code
Yii::$app->session->keyPrefix . md5(json_encode([__CLASS__, Yii::$app->session->id]))
taken from official code
https://github.com/yiisoft/yii2-redis/blob/master/...
/**
     * Generates a unique key used for storing session data in cache.
     * @param string $id session variable name
     * @return string a safe cache key associated with the session variable name
     */
    protected function calculateKey($id)
    {
        return $this->keyPrefix . md5(json_encode([__CLASS__, $id]));
    }


How to get redis key from session_id() - 8878353c8ca97c34021d62279cc7959a - QAINAR_SESS_fd4011b394ac47f32666d77e2ecd7e6d ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question