Yii memcache session got invalidated after deployment
php, yii
Solution
By default `CCache` will use a `keyPrefix` that contains `Yii::app()->id`. This unique `ID` is calculated as a hash of the current `basePath` plus the `name` of the application. If you look at `setBasePath()` you see, that it will use the `realpath()`. This causes symlinks to be resolved to their origin.
So if the origin of your symlink changes, this will lead to a different application ID, which again leads to a changed cache key prefix. And this invalidates your cache content.
To fix this, you can either
- Set a static `id` on the application in your `main.php` configuration, or
- Set a static `keyPrefix` on your `memcache` component.
The latter is recommended anyway, if you have more than one server that all should access the same memcached pool.
Problem
Every time I deploy my Yii app, I change the symbolic link of the `/var/www`. Something similar like this ``` rm -f /var/www ln -s /var/app-version /var/www ``` But every time I do this, the user sessions got invalidated (i.e. all of the users got logged out and CSRF tokens were reset). For the session I use `CCacheHttpSession`. Something like below in the `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 whether this misconfiguration is in PHP-level or Yii-level, but what did I do wrong?