Yii stores the session to memcached under Web distribution

Source: Internet
Author: User
Tags php session yii


When the visit of the website is growing, a machine can not support burst, or is to do a single point, all need to build a cluster in the backend to handle the user's request, because the traditional PHP session is file-level storage, then if a user in the first logon when the session file exists a Server, and the second time is divided into the B server, then think he is not logged in (of course, the load balancer is configured to allow the same user forever on the same machine, this can be skipped ...) ), so we need to place it somewhere else, I choose the memcached, there is, of course, the latter may choose Redis because it can be more accurate in terms of value, provincial network bandwidth.

In Yii I want to achieve the session information stored in the memcached only need a little configuration, my current Yii version is yii-1.1.13.e9e4a0

' Session ' = = Array (' class ' = ' ccachehttpsession ', ' autoStart ' = ' = ', ' cacheid ' + ' SE  Ssioncache ',///We only use the Sessioncache to store the session ' cookiemode ' = ' only ', ' timeout ' =                1400,), ' sessioncache ' = = Array (' class ' = ' System.caching.CMemCache ', ' servers ' = = Array (                        Array (' host ' = ' 192.168.10.193 ', ' port ' = 11211, ' Weight ' = 6, Array (' host ' = ' 192.168.10.226 ') , ' port ' = 11211, ' weight ' + 3), Arra                        Y (' host ' = ' 192.168.10.228 ', ' port ' = 11211, ' Weight ' = 2),),

Test login no problem, and then very curious, it really deposited into the memcached inside it? How can we verify it?

In order to save a new controller, I wrote an action directly to sitecontroller inside.

The Public Function actiontestsessionwithmemcached () {/         * * Gets         the sessionid number * to calculate the existence of the Memcached key value.         *        /$sessionId = Yii::app ()->session->sessionid;        echo "key:", $key = Ccachehttpsession::cache_key_prefix. $sessionId;                 /**         * This is equivalent to using the memcached connection directly, and the session does not have any hooks,         * Let's see if the session data really exists in the memcached inside.         * The calculated key is obtained directly with the GET command and then the data is printed out to see the value.         * Log in when testing Oh, do not log on to start the test estimate will not get the value, think there is a problem!         *        /$mem =  Yii::app ()->sessioncache;        $data = $mem->get ($key);        Var_dump ($data);}


Yii stores the session to memcached under Web distribution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.