Some questions about the session and the Memcache

Source: Internet
Author: User
Tags session id

The function of the session is basically achieved by
* Set the client's cookie to save the SessionID,
* Then save the user's data on the server side, and finally

* The session ID in the cookie to determine if a data is a user,


Original
Session.save_handler = Files


Mem:

Session.save_handler = Memcache

Session.save_path = "tcp://192.168.0.26:13001,tcp://192.168.0.26:13002"

Session.use_cookies = 1


Memcache Configuration Array
$memcache _servers=array (
Array ("host" = "192.168.0.26",
"Port" = "14001",
"Persistent" =>true,
"Weight" =>1,
"Timeout" =>1,//1 is the default value in seconds
"Retry_interval" =>2,
"Status" =>true,
"Failure_callback" = "Memcacheconnectlog"
),
Array ("host" = "192.168.0.26",
"Port" = "14002",
"Persistent" =>true,
"Weight" =>1,
"Timeout" =>1,//1 is the default value in seconds
"Retry_interval" =>2,
"Status" =>true,
"Failure_callback" = "memcacheconnectlog"//write Log function
)
);

Since the session mechanism of PHP itself can not be cross-machine, so many phper feel uncomfortable, now popular solutions mainly include:
1) Use the database to implement
2) write the server side by itself and rewrite the session handler function to request
3) Save session with NFS and other cross-machine storage
4) use Memcache to save
5) Use the solution provided by Zend Platform

1-4 of these are through the use of the storage mechanism can be cross-machine, and then use Session_set_save_handler () to achieve, 5 is Zend Company's commercial products (but according to former colleagues in the use of reflection, the effect is not very satisfactory), the above scheme, each has pros and cons, not in the scope of this article

Whether using memcache, or DB,NFS, the principle is the same, all through the Session_set_save_handler function to change the default processing mode, by specifying a callback function to customize the processing, you can refer to the manual session_set_save_ Handler () function section, there are examples, it is easier to understand

Here are some of the records that I realized when I was using memcache:
1) When implemented using a class, each callback function is defined as a static method, using Session_set_save_handler to register the callback function in the construction of the class, such as:
Session_set_save_handler (
Array (' memsession ', ' open '),
Array (' memsession ', ' close '),
Array (' memsession ', ' read '),
Array (' memsession ', ' write '),
Array (' memsession ', ' destroy '),
Array (' memsession ', ' GC ')
);
Memsession for the class name, to use the session, the first new memsession, and then Session_Start ();


2) lifetime and garbage collection
Memcache's SET command has a lifetime, that is, when adding a value using the SET command, you can add lifetime, which can be used as the lifetime of the session, the user has no action during this time, it will fail, but the action will not fail (because each script ends, Will execute write and close, lifetime will be updated), and of course, if the SID is passed with a cookie, the control session lifetime can be used: ini_set (' Session.cookie_lifetime ', time) To set, this is actually the effective time to control the cookie, if the session depends on the survival of the cookie disappears, of course, the session will not live, using cookie_lifetime to control, whether there is no action, will be obsolete after the specified time

GC refers to garbage collection, which in session refers to the cleanup of expired session data, and the parameters that are affected are:
Session.gc_maxlifetime is considered a survival period before the garbage, the data will be cleared away if there is no action at this time
Note that the GC is not executed every time the boot session is performed, but is determined by the ratio of session.gc_probability to session.gc_divisor.

Conclusion: There are several methods to control the survival period of the session
The first is the Cookie_lifttime, which will be destroyed within a specified time, whether or not it is in action.
The second is in read in the root of the save time control, this method in the action time will always be effective
Three sets the ratio of session.gc_probability and session.gc_divisor to 1 (that is, each session is enabled GC), and then set the Gc.maxlifetime to specify the lifetime, this method is also effective when the user has action time

3) Timing of callback function execution
Open is executed when running Session_Start ()
Read executes when run Session_Start (), because at session_start time, it goes to read the current session data and writes the $_session variable
Destroy execution when running Session_destroy ()
Close is executed when the script executes or calls Session_write_close () or Session_destroy (), which is executed after all session operations are completed
GC execution probabilities are determined by the values of session.gc_probability and session.gc_divisor, and the timing is after open,read, that is, Session_Start will execute Open,read and GC successively
Write this method executes when the script ends and uses session_write_close () to force the session data to be committed

Conclusion:
Session_Start//Execute open (start session), read (read session data to $_session), GC (garbage Cleanup)
None of the actions for $_session in the middle of the script call these callback functions
Session_destroy//execute Destroy, destroy the current session (usually delete the corresponding record or file), accordingly, this callback function destroys only session data, but at this time

Var_dump $_session variable, still has value, but this value will not be write back after close
Session_write_close ()//execute write and close, save $_session to storage, if this method is not manually used, it will be executed automatically at the end of the script

The clarity of the above information will help you understand clearly how the session works ...

4) Direct use of memcache for session processing
After I wrote a series of Memcache to save the session code, I inadvertently found that I can set the use of Memcache as a session in the php.ini, without having to encode another method:
Modify the following values in the PHP.ini
Session.save_handler = Memcache
Session.save_path = ' tcp://host1:11211 ' #有多个时直接用 "," separated by
If you only want to use Memcache storage session in a specific application, you can use the Ini_set method to set the above two parameters

To test if you really use the memcache, you can catch enough to use the PHPSESSID, and then as key with Memcach to read, it is clear

Some questions about the session and the Memcache

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.