Php session is optimal for writing information to memcache for management.

Source: Internet
Author: User
Tags php session

Php session is optimal for writing information to memcache for management.

We have also discussed the methods and benefits of using memcache to store data information cache, which can reduce the number of visits to the database and reduce the pressure on the database when the access volume is large.

To store sessions in memcache for management, you need to understand the usage of memcache and sessions and session_set_save_handler ()

Write a public class first. Of course, the static member method is used.

Use telnet for memcache commands

 

Similarly, you need to create files in the root directory.

Among them, memsession. class. php is a public memcache storage class file, one. php, two. php, and three. php are test files, and items. php is an output data array.

Session. class. php:

First, define the variables used to connect to memcache and initialize

 

 Connect ("localhost", 11211) or die ("cocould not connect"); MemSession: start ($ memcache );


 

 

Note thatNS is a constant and a subscript is defined.

 

Reinitialization Method

 

// Initialization method private static function init ($ handler) {self ::$ handler = $ handler; self ::$ lifetime = ini_get ('session. gc_maxlifetime '); self: $ time = time ();}


 

Enable the session and define the call methods such as open and close in this class.

 

// Enable session public static function start (Memcache $ memcache) {// first initialize the property self: init ($ memcache); // call handler, when handler is called in the future, memcache session_set_save_handler (array (_ CLASS __, 'open') will be used to call the open method array (_ CLASS __, 'close'), array (_ CLASS __, 'read'), array (_ CLASS __, 'write'), array (_ CLASS __, 'deststroy'), array (_ CLASS __, 'gc '); // call session_start ();}

The next step is to define the methods called above

 

Open () and close () only need to return true,Open () parameters are path and name)

 

    public static function open($path, $name){    return true;    }    public static function close(){    return true;    }

Read () only requires the PHPSESSID ParameterYou can.

 

However, to determine whether the input out parameter has a value, the output data is returned if it has a value.

 

Public static function read ($ PHPSESSID) {$ out = self ::$ handler-> get (self: session_key ($ PHPSESSID )); // obtain the data output by this subscript. if ($ out = false | $ out = null) {return ''; // out, no data is obtained, returned null} return $ out; // returned data}

Write ():

 

Returns its own id, data, and lifecycle.

 

Public static function write ($ PHPSESSID, $ data) {// determines whether data exists $ method = $ data? 'Set': 'relpace'; return self: $ handler-> $ method (self: session_key ($ PHPSESSID), $ data, MEMCACHE_COMPRESSED, self :: $ lifetime );}

 

 

Destroy () and gc ():

Destroy () calls its own delete Method

 

Public static function destroy ($ PHPSESSID) {return self: $ handler-> delete (self: session_key ($ PHPSESSID )); // call the delete method} public static function gc ($ lifetime) {return true ;}

Next, you need to define a method for passing in PHPSESSID.

 

 

Private static function session_key ($ PHPSESSID) {$ session_key = self: NS. $ PHPSESSID; // the key value is itself and the input phpsessid return $ session_key ;}

 

 

 

Result Display

If yes, it is displayed in telnet.

 

 

The session data is successfully stored in 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.