Optimize the use of mysql to store Sessions. Read and optimize the use of mysql to store Sessions. I have written two articles: Custom session (II) -- database saving and why I don't use session, but later I found all problems. The former processing is practically useless in practice, and the session collection has to be processed by itself. The latter frequently operates the database "> <LINKhref =" h
I have written two articles, "Custom SESSION (2) -- database storage" and "Why I don't use session".
However, all problems were found later. The former processing is practically useless in practice, and the session collection has to be processed by itself. The latter frequently operates the database, causing great performance problems.
After careful consideration over the past two days, a general solution is provided, but no detailed tests are available.
1. combine session processing and statistics. Visitors also have records.
2. fully use the database and cookie to simulate the session function.
3. the user's session operations should be completed in an SQL statement whenever possible. When the session is not used, there will be no more queries.
4. for the sake of efficiency, session recycling is not integrated, but an interface is provided and can be called for implementation.
The code is provided for the time being, with no specific explanation.
SQL
Create table ***** _ session '(
'Sid 'char (32) not null,
'Uid' int (10) not null,
'Username' char (32) not null,
'Usertype' tinyint (1) not null,
'Activetime' int (10) not null,
'Expiry' int (10) not null,
'IP' char (15) not null,
'URL' char (80) not null,
'Value' char (255) not null,
Primary key ('Sid ')
) ENGINE = memory default charset = utf8;
Php code
Class session {
Private $ _ sessionPrex = ''; // session prefix
Private $ _ time = ''; // current time
Private $ _ model = null; // database operation model
Private $ _ expiry = 1200; // session validity period
Private $ _ domain = ''; // The scope of the session
Protected $ isNew = 0; // the action for determining the operation is 0. Update 1 is added.
Protected $ session = array (); // A corresponding session record
Public function _ construct ($ options ){
$ This-> _ setOptions ($ options );
If (empty ($ this-> _ time) $ this-> _ time = time ();
$ This-> session ['activetime'] = $ this-> _ time;
}
Public function start (){
$ This-> _ getSid ();
}
Public function set ($ key, $ value ){
If (in_array ($ key, array ('uid', 'username', 'usertype', 'URL', 'expiry '))){
If ($ key = 'expiry '){
$ This-> _ setCookie ($ this-> _ sessionPrex. '_ sid', $ this-> session ['Sid '], $ value );
$ This-> _ setCookie ($ this-> _ sessionPrex. '_ uid', $ this-> session ['uid'], $ value );
}
$ This-> session [$ key] = $ value;
} Else {
$ Other = $ this-> session ['value'];
$ Other [$ key] = $ value;
$ This-> session ['value'] = $ other;
}
}
Public function get ($ key ){
If (in_array ($ key, array ('uid', 'username', 'usertype', 'URL', 'expiry '))){
Return $ this-> session [$ key];
} Else {
If (isset ($ this-> session ['value'] [$ key]) {
Return $ this-> session ['value'] [$ key];
}
Return null;
}
}
Public function gc ($ file, $ time = 1200 ){
$ Lasttime = file_get_contents ($ file );
If ($ lasttime + $ time <$ this-> _ time ){
File_put_contents ($ file, $ this-> _ time );
Return $ this-> _ model-> delete ('activetime + expiry <'. $ this-> _ time );
}
}
Public function destroy (){
$ This-> session ['uid'] = 0;
$ This-> session ['username'] = '';
$ This-> session ['usertype'] =-1;
$ This-> session ['expiry'] = $ this-> _ expiry;
$ This-> session ['value'] = array ();
$ This-> _ setCookie ($ this-> _ sessionPrex. '_ sid', $ this-> session ['Sid '], $ this-> _ expiry );
$ This-> _ setCookie ($ this-> _ sessionPrex. '_ uid', $ this-> session ['uid'], $ this-> _ expiry );
}
Public function _ destruct (){
$ This-> _ save ();
}
Private function _ save (){
$ DbSession = $ this-> session;
$ DbSession ['value'] = serialize ($ dbSession ['value']);
If (strlen ($ dbSession ['value'])> 255) $ this-> _ error ('session-> value is too long! ');
If ($ this-> isNew = 1 ){
// Add
$ This-> _ model-> insert ($ dbSession );
} Else {
// Update
$ Sid = $ dbSession ['Sid '];
$ This-> _ model-> update (array_slice ($ dbSession, 1), 'Sid = \ ''. $ sid .'\'');
}
}
Private function _ getSession ($ sid ){
$ DbSession = $ this-> _ model-> detail ('Sid = \ ''. $ sid .'\'');
If (! $ DbSession) return false;
$ DbSession ['value'] = unserialize ($ dbSession ['value']);
$ This-> session = array_merge ($ dbSession, $ this-> session );
Return true;
}
Private function _ getSid (){
$ Sid = strip_tags ($ _ COOKIE [$ this-> _ sessionPrex. '_ sid']);
If (strlen ($ sid) = 32 ){
If ($ this-> _ getSession ($ sid )){
Return true;
}
} Else {
$ Sid = md5 (time (). mt_rand (1000,10000 ));
$ This-> _ setCookie ($ this-> _ sessionPrex. '_ sid', $ sid );
}
$ This-> _ setCookie ($ this-> _ sessionPrex. '_ uid', 0 );
$ This-> session = array (
'Uid' => 0,
'Username' => '',
'Usertype' =>-1,
'Activetime' => $ this-> _ time,
'IP' => $ this-> _ getip (),
'URL' => strip_tags ($ _ SERVER ['request _ URI ']),
'Expiry' => $ this-> _ expiry,
'Value' => array ()
);
$ This-> isNew = 1;
$ This-> session ['Sid '] = $ sid;
}
Private function _ setCookie ($ name, $ value, $ expiry = 0 ){
If (empty ($ expiry) $ expiry = $ this-> _ expiry;
If (empty ($ this-> _ domain )){
Setcookie ($ name, $ value, $ this-> _ time + $ expiry ,'/');
} Else {
Setcookie ($ name, $ value, $ this-> _ time + $ expiry, '/', $ this-> _ domain );
}
}
Private function _ getip (){
Return getip ();
}
Private function _ setOptions ($ options ){
Foreach ($ options as $ key => $ value ){
If (in_array ($ key, array ('sessionprex', 'time', 'model', 'expiry', 'domain '))){
$ Key = '_'. $ key;
$ This-> $ key = $ value;
}
}
}
Private function _ error ($ msg ){
Throw new Phpbean_Exception ($ msg );
}
}
?>
(Note: This code cannot be used directly. This article mainly provides an idea)