Memcache.class.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Think\Session\Driver;
  3. class Memcache {
  4. protected $lifeTime = 3600;
  5. protected $sessionName = '';
  6. protected $handle = null;
  7. /**
  8. * 打开Session
  9. * @access public
  10. * @param string $savePath
  11. * @param mixed $sessName
  12. */
  13. public function open($savePath, $sessName) {
  14. $this->lifeTime = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') : $this->lifeTime;
  15. // $this->sessionName = $sessName;
  16. $options = array(
  17. 'timeout' => C('SESSION_TIMEOUT') ? C('SESSION_TIMEOUT') : 1,
  18. 'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : 0
  19. );
  20. $this->handle = new \Memcache;
  21. $hosts = explode(',', C('MEMCACHE_HOST'));
  22. $ports = explode(',', C('MEMCACHE_PORT'));
  23. foreach ($hosts as $i=>$host) {
  24. $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
  25. $this->handle->addServer($host, $port, true, 1, $options['timeout']);
  26. }
  27. return true;
  28. }
  29. /**
  30. * 关闭Session
  31. * @access public
  32. */
  33. public function close() {
  34. $this->gc(ini_get('session.gc_maxlifetime'));
  35. $this->handle->close();
  36. $this->handle = null;
  37. return true;
  38. }
  39. /**
  40. * 读取Session
  41. * @access public
  42. * @param string $sessID
  43. */
  44. public function read($sessID) {
  45. return $this->handle->get($this->sessionName.$sessID);
  46. }
  47. /**
  48. * 写入Session
  49. * @access public
  50. * @param string $sessID
  51. * @param String $sessData
  52. */
  53. public function write($sessID, $sessData) {
  54. return $this->handle->set($this->sessionName.$sessID, $sessData, 0, $this->lifeTime);
  55. }
  56. /**
  57. * 删除Session
  58. * @access public
  59. * @param string $sessID
  60. */
  61. public function destroy($sessID) {
  62. return $this->handle->delete($this->sessionName.$sessID);
  63. }
  64. /**
  65. * Session 垃圾回收
  66. * @access public
  67. * @param string $sessMaxLifeTime
  68. */
  69. public function gc($sessMaxLifeTime) {
  70. return true;
  71. }
  72. }