Write the Memcache-php Tutorial to the Session

Source: Internet
Author: User
Write sessions to Memcache using session_set_save_handler () to customize the Session write into Memcache1 & lt ;? Php2classMemSession {3 privatestatic $ handlernull; 4 privatestatic $ li writes the Session to Memcache

Use session_set_save_handler () to customize the Session to write data to Memcache

1
 Get (self: session_key ($ PHPSESSID); 37 38 if ($ val = false | $ val = null) {39 return false; 40} 41 return $ val; 42} 43 public static function write ($ PHPSESSID, $ data) {44 $ method = $ data? 'Set': 'Replace '; 45 return self: $ handler-> $ method (self: session_key ($ PHPSESSID), $ data, MEMCACHE_COMPRESSED, self :: $ lifetime); 46} 47 48 public static function destroy ($ PHPSESSID) {49 return self: $ handle-> delete (self: session_key ($ PHPSESSID )); 50} 51 // memcache has a limited time and data is automatically destroyed. Therefore, you do not need to use the gc method 52 public static function gc ($ lifetime) {53 return true; 54} 55 56 // prefix sessionID to avoid repeated key 57 private static function session_key ($ PHPSESSID) {58 $ session_key = self: MS. $ PHPSESSID; 59 return $ session_key; 60} 61} 62 $ mem = new Memcache; 63 $ mem-> connect ("localhost", 11211) or die ("cocould not connect"); 64 MemSession: start ($ mem );

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.