BVB Source Codes

DolpinPHP v1.0.0 rapid development framework Show Memcache.php Source code

Return Download DolpinPHP v1.0.0 rapid development framework: download Memcache.php Source code - Download DolpinPHP v1.0.0 rapid development framework Source code - Type:.php
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11.  
  12. namespace think\session\driver;
  13.  
  14. use SessionHandler;
  15. use think\Exception;
  16.  
  17. class Memcache extends SessionHandler
  18. {
  19.     protected $handler = null;
  20.     protected $config  = [
  21.         'host'         => '127.0.0.1', // memcache主机
  22.         'port'         => 11211, // memcache端口
  23.         'expire'       => 3600, // session有效期
  24.         'timeout'      => 0, // 连接超时时间(单位:毫秒)
  25.         'persistent'   => true, // 长连接
  26.         'session_name' => '', // memcache key前缀
  27.     ];
  28.  
  29.     public function __construct($config = [])
  30.     {
  31.         $this->config = array_merge($this->config, $config);
  32.     }
  33.  
  34.     /**
  35.      * 打开Session
  36.      * @access public
  37.      * @param string    $savePath
  38.      * @param mixed     $sessName
  39.      */
  40.     public function open($savePath, $sessName)
  41.     {
  42.         // 检测php环境
  43.         if (!extension_loaded('memcache')) {
  44.             throw new Exception('not support:memcache');
  45.         }
  46.         $this->handler = new \Memcache;
  47.         // 支持集群
  48.         $hosts = explode(',', $this->config['host']);
  49.         $ports = explode(',', $this->config['port']);
  50.         if (empty($ports[0])) {
  51.             $ports[0] = 11211;
  52.         }
  53.         // 建立连接
  54.         foreach ((array) $hosts as $i => $host) {
  55.             $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
  56.             $this->config['timeout'] > 0 ?
  57.             $this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']) :
  58.             $this->handler->addServer($host, $port, $this->config['persistent'], 1);
  59.         }
  60.         return true;
  61.     }
  62.  
  63.     /**
  64.      * 关闭Session
  65.      * @access public
  66.      */
  67.     public function close()
  68.     {
  69.         $this->gc(ini_get('session.gc_maxlifetime'));
  70.         $this->handler->close();
  71.         $this->handler = null;
  72.         return true;
  73.     }
  74.  
  75.     /**
  76.      * 读取Session
  77.      * @access public
  78.      * @param string $sessID
  79.      */
  80.     public function read($sessID)
  81.     {
  82.         return $this->handler->get($this->config['session_name'] . $sessID);
  83.     }
  84.  
  85.     /**
  86.      * 写入Session
  87.      * @access public
  88.      * @param string    $sessID
  89.      * @param String    $sessData
  90.      */
  91.     public function write($sessID, $sessData)
  92.     {
  93.         return $this->handler->set($this->config['session_name'] . $sessID, $sessData, 0, $this->config['expire']);
  94.     }
  95.  
  96.     /**
  97.      * 删除Session
  98.      * @access public
  99.      * @param string $sessID
  100.      */
  101.     public function destroy($sessID)
  102.     {
  103.         return $this->handler->delete($this->config['session_name'] . $sessID);
  104.     }
  105.  
  106.     /**
  107.      * Session 垃圾回收
  108.      * @access public
  109.      * @param string $sessMaxLifeTime
  110.      */
  111.     public function gc($sessMaxLifeTime)
  112.     {
  113.         return true;
  114.     }
  115. }
  116.  
downloadMemcache.php Source code - Download DolpinPHP v1.0.0 rapid development framework Source code
Related Source Codes/Software:
HTML 5 responsive weave template v1.0 dream relaxed and blog posts - HTML5 response-refreshing blog dream template is a... 2017-05-06
Responsive precision chemical chemical class dreams to template (adaptive equipment) v1.0 - Response precision chemical chemical dream templat... 2017-05-06
Dream weaving turquoise ankang health information web site template v1.0 - Dream green health health information website full... 2017-05-05
Cisco v1.1 CMS backend content management system - Cisco CMS back-end content management system is a ... 2017-05-05
HTML 5 black reactive booth design dream weaving dedecms site template (adaptive) v1.0 - HTML5 black-response design dream dedecms template... 2017-05-06
WSTShop v1.1.0 B2C revenue system - WSTShop B2C revenue system WSTShop e-commerce syst... 2017-05-06
HTML 5 high-end dedecms decorate construction company enterprise template - Dedecms latest kernel source code, this source cod... 2017-05-06
compromise - Natural language processing in javascript ... 2017-06-05
stack-on-a-budget - A collection of services with great free tiers for... 2017-06-05
packer - Packer is a tool for creating identical machine im... 2017-06-05
pkg - Package your Node.js project into an executable ... 2017-06-05
Dapper - Dapper - a simple object mapper for .Net 2017-06-05
python-fire - Python Fire is a library for automatically generat... 2017-06-04
kaminari - A Scope & Engine based, clean, powerful, custo... 2017-06-04
mongo-express - Web-based MongoDB admin interface, written with No... 2017-05-17
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16

 Back to top