PHP Session Best writes information to Memcache management
The previous approach and benefits of storing data information caches with Memcache can reduce the number of accesses to the database and reduce the stress on the database when the traffic is large.
Store session to Memcache management needs to understand memcache, session usage, and Session_set_save_handler ()
Also write a common class first, of course, using static member methods
Memcache command with Telnet operation
Also now create the required files in the root directory
Where memsession.class.php is a common Memcache storage class file, one.php, two.php, and three.php are test files, items.php is an array of output data
In session.class.php:
First define the variables used to connect the Memcache and initialize
Connect ("localhost", 11211) or die ("could not Connect"); Memsession::start ($memcache);
Note that NS is a constant, which defines the subscript
Re-initialize Method
Initialization method private static function init ($handler) {self :: $handler = $handler; Self:: $lifetime =ini_get (' session.gc_maxlifetime '); Self:: $time =time (); }
Open the session and define the method called open, close, etc. in this class
Open session public static function start (Memcache $memcache) { //First initialize the property self::init ($memcache); Call handler and call handler later with Memcache Session_set_save_handler ( array (__class__, ' open '),//Call the Open method of this class Array ( __class__, ' close '), Array ( __class__, ' read '), array ( __class__, ' write '), array ( __class__ , ' Destroy '), Array (__class__, ' GC ') ); Call Session_Start () session_start (); }
The next step is to define the methods called above
Open () and close () as long as the return is true, but the argument of open () is the path and name (name)
public static function open ($path, $name) { return true; } public static function Close () { return true; }
read () only need to have PHPSESSID parameterCan
But to determine if the incoming out parameter has a value, a value returns the out data
public static function read ($PHPSESSID) { $out =self:: $handler->get (Self::session_key ($PHPSESSID)); Get the data for the subscript output if ($out ===false | | $out ==null) { return '; Out get data no, return empty } return $out; Return the resulting data }
Write ():
Returns the ID, data, and duration of the life itself
public static function Write ($PHPSESSID, $data) { //To determine if there is data $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;//key value for itself and passed in Phpsessid return $session _key; }
Results show
If successful, display in Telnet
Indicates that session data information is stored to memcache success
http://www.bkjia.com/PHPjc/971083.html www.bkjia.com true http://www.bkjia.com/PHPjc/971083.html techarticle PHP Session best to write information to Memcache in front of the management also described the use of memcache to store data information caching methods and benefits, so as to reduce the number of access to the database, reduce access ...