Yii memcache session invalidated after deployment

Each time I deploy a Yii application, I change the /var/www symbolic link. Something like this

 rm -f /var/www ln -s /var/app-version /var/www 

But every time I do this, user sessions become invalid (i.e. all users are logged out, and CSRF tokens were reset).

For the session, I use CCacheHttpSession . Something like below in main.php

 'components' => [ 'memcache' => [ 'class' => 'CMemCache', 'servers' => [ [ 'host' => 'localhost', 'port' => 11211, ] ] ], 'user' => [ 'class' => 'WebUser', 'allowAutoLogin' => true, ], 'session' => [ 'class' => 'CCacheHttpSession', 'cacheID' => 'memcache' ] ] 

I'm not sure if this incorrect configuration is at the PHP or Yii level, but what did I do wrong?

+4
source share
1 answer

By default, CCache will use keyPrefix , which contains Yii::app()->id . This unique ID calculated as the hash of the current basePath plus the name application. If you look at setBasePath() , you will see that it will use realpath() . This leads to the fact that symbolic links are resolved by their origin.

So, if the origin of your symbolic link changes, it will lead to a different application identifier, which again will lead to a change in the cache key prefix. And this is not valid for your cache contents.

To fix this, you can either

  • Set a static ID in the application in your main.php configuration or
  • Install static keyPrefix in your memcache component.

The latter is recommended in any case if you have several servers that must have access to the same memcached file.

+8
source

Source: https://habr.com/ru/post/1498119/


All Articles