ecshop的session放入memcache

2016-07-07 14:55 来源:www.chinab4c.com 作者:ecshop专家



经过很多朋友反馈,ecshop的session表在高并发下。容易提示is full错误。所以我们考虑将ecshop session放入memcache中。大概粗略写了个流程。运行目前没发现问题。把代码粘贴出来。大家一起来完善这个功能
  1. <?php

  2. if (!defined('IN_ECS'))
  3. {
  4. die('Hacking attempt');
  5. }

  6. class cls_session
  7. {
  8. var $db = NULL;
  9. var $session_table = '';

  10. var $max_life_time = 1800; // SESSION 过期时间

  11. var $session_name = '';
  12. var $session_id = '';

  13. var $session_expiry = '';
  14. var $session_md5 = '';

  15. var $session_cookie_path = '/';
  16. var $session_cookie_domain = '';
  17. var $session_cookie_secure = false;
  18. var $memcache = null;
  19. var $_ip = '';
  20. var $_time = 0;

  21. function __c**truct(&$db, $session_table, $session_data_table, $session_name = 'ECS_ID', $session_id = '')
  22. {
  23. $this->cls_session($db, $session_table, $session_data_table, $session_name, $session_id);
  24. }

  25. function cls_session(&$db, $session_table, $session_data_table, $session_name = 'ECS_ID', $session_id = '')
  26. {

  27. $this->memcache = new Memcache();
  28. $this->memcache->connect("127.0.0.1","11211");
  29. $GLOBALS['_SESSION'] = array();

  30. if (!empty($GLOBALS['cookie_path']))
  31. {
  32. $this->session_cookie_path = $GLOBALS['cookie_path'];
  33. }
  34. else
  35. {
  36. $this->session_cookie_path = '/';
  37. }

  38. if (!empty($GLOBALS['cookie_domain']))
  39. {
  40. $this->session_cookie_domain = $GLOBALS['cookie_domain'];
  41. }
  42. else
  43. {
  44. $this->session_cookie_domain = '';
  45. }

  46. if (!empty($GLOBALS['cookie_secure']))
  47. {
  48. $this->session_cookie_secure = $GLOBALS['cookie_secure'];
  49. }
  50. else
  51. {
  52. $this->session_cookie_secure = false;
  53. }

  54. $this->session_name = $session_name;
  55. $this->session_table = $session_table;
  56. $this->session_data_table = $session_data_table;

  57. $this->db = &$db;
  58. $this->_ip = real_ip();

  59. if ($session_id == '' && !empty($_COOKIE[$this->session_name]))
  60. {
  61. $this->session_id = $_COOKIE[$this->session_name];
  62. }
  63. else
  64. {
  65. $this->session_id = $session_id;
  66. }

  67. if ($this->session_id)
  68. {
  69. $tmp_session_id = substr($this->session_id, 0, 32);
  70. if ($this->gen_session_key($tmp_session_id) == substr($this->session_id, 32))
  71. {
  72. $this->session_id = $tmp_session_id;
  73. }
  74. else
  75. {
  76. $this->session_id = '';
  77. }
  78. }

  79. $this->_time = time();

  80. if ($this->session_id)
  81. {
  82. $this->load_session();
  83. }
  84. else
  85. {
  86. $this->gen_session_id();

  87. setcookie($this->session_name, $this->session_id . $this->gen_session_key($this->session_id), 0, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);
  88. }

  89. register_shutdown_function(array(&$this, 'close_session'));
  90. }

  91. function gen_session_id()
  92. {
  93. $this->session_id = md5(uniqid(mt_rand(), true));

  94. return $this->insert_session();
  95. }

  96. function gen_session_key($session_id)
  97. {
  98. static $ip = '';

  99. if ($ip == '')
  100. {
  101. $ip = substr($this->_ip, 0, strrpos($this->_ip, '.'));
  102. }

  103. return sprintf('%08x', crc32(!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] . ROOT_PATH . $ip . $session_id : ROOT_PATH . $ip . $session_id));
  104. }

  105. function insert_session()
  106. {
  107. $ar = array('expiry'=>$this->_time, 'ip'=>$this->_ip, 'data'=>'a:0:{}','userid'=>'0','adminid'=>'0','user_name'=>'','user_rank'=>'','discount'=>'','email'=>'');
  108. return $this->memcache->set($this->session_id, $ar, false, $this->max_life_time);

  109. }

  110. function load_session()
  111. {
  112. $session = $this->memcache->get($this->session_id);
  113. //print_r($session);
  114. if (empty($session))
  115. {
  116. $this->insert_session();

  117. $this->session_expiry = 0;
  118. $this->session_md5 = '40cd750bba9870f18aada2478b24840a';
  119. $GLOBALS['_SESSION'] = array();
  120. }
  121. else
  122. {
  123. if (!empty($session['data']) && $this->_time - $session['expiry'] <= $this->max_life_time)
  124. {
  125. $this->session_expiry = $session['expiry'];
  126. $this->session_md5 = md5(base64_decode($session['data']));

  127. $GLOBALS['_SESSION'] = unserialize(base64_decode($session['data']));
  128. $GLOBALS['_SESSION']['user_id'] = $session['userid'];
  129. $GLOBALS['_SESSION']['admin_id'] = $session['adminid'];
  130. $GLOBALS['_SESSION']['user_name'] = $session['user_name'];
  131. $GLOBALS['_SESSION']['user_rank'] = $session['user_rank'];
  132. $GLOBALS['_SESSION']['discount'] = $session['discount'];
  133. $GLOBALS['_SESSION']['email'] = $session['email'];
  134. }
  135. else
  136. {
  137. $session_data = $this->db->getRow('SELECT data, expiry FROM ' . $this->session_data_table . " WHERE sesskey = '" . $this->session_id . "'");
  138. if (!empty($session_data['data']) && $this->_time - $session_data['expiry'] <= $this->max_life_time)
  139. {
  140. $this->session_expiry = $session_data['expiry'];
  141. $this->session_md5 = md5($session_data['data']);

  142. $GLOBALS['_SESSION'] = unserialize(base64_decode($session['data']));
  143. $GLOBALS['_SESSION']['user_id'] = $session['userid'];
  144. $GLOBALS['_SESSION']['admin_id'] = $session['adminid'];
  145. $GLOBALS['_SESSION']['user_name'] = $session['user_name'];
  146. $GLOBALS['_SESSION']['user_rank'] = $session['user_rank'];
  147. $GLOBALS['_SESSION']['discount'] = $session['discount'];
  148. $GLOBALS['_SESSION']['email'] = $session['email'];
  149. }
  150. else
  151. {
  152. $this->session_expiry = 0;
  153. $this->session_md5 = '40cd750bba9870f18aada2478b24840a';
  154. $GLOBALS['_SESSION'] = array();
  155. }
  156. }
  157. }
  158. }

  159. function update_session()
  160. {
  161. $adminid = !empty($GLOBALS['_SESSION']['admin_id']) ? intval($GLOBALS['_SESSION']['admin_id']) : 0;
  162. $userid = !empty($GLOBALS['_SESSION']['user_id']) ? intval($GLOBALS['_SESSION']['user_id']) : 0;
  163. $user_name = !empty($GLOBALS['_SESSION']['user_name']) ? trim($GLOBALS['_SESSION']['user_name']) : 0;
  164. $user_rank = !empty($GLOBALS['_SESSION']['user_rank']) ? intval($GLOBALS['_SESSION']['user_rank']) : 0;
  165. $discount = !empty($GLOBALS['_SESSION']['discount']) ? round($GLOBALS['_SESSION']['discount'], 2) : 0;
  166. $email = !empty($GLOBALS['_SESSION']['email']) ? trim($GLOBALS['_SESSION']['email']) : 0;
  167. unset($GLOBALS['_SESSION']['admin_id']);
  168. unset($GLOBALS['_SESSION']['user_id']);
  169. unset($GLOBALS['_SESSION']['user_name']);
  170. unset($GLOBALS['_SESSION']['user_rank']);
  171. unset($GLOBALS['_SESSION']['discount']);
  172. unset($GLOBALS['_SESSION']['email']);

  173. $data = base64_encode(serialize(($GLOBALS['_SESSION'])));
  174. $this->_time = time();

  175. if ($this->session_md5 == md5($data) && $this->_time < $this->session_expiry + 10)
  176. {
  177. return true;
  178. }

  179. $data = addslashes($data);

  180. if (isset($data{255}))
  181. {
  182. }

  183. $ar = array('expiry'=>$this->_time, 'ip'=>$this->_ip, 'data'=>$data,'userid'=>$userid,'adminid'=>$adminid,'user_name'=>$user_name,'user_rank'=>$user_rank,'discount'=>number_format($discount,2,".",""),'email'=>$email);

  184. return $this->memcache->replace($this->session_id, $ar, false, $this->max_life_time);


  185. }

  186. function close_session()
  187. {
  188. $this->update_session();


  189. return true;
  190. }

  191. function delete_spec_admin_session($adminid)
  192. {
  193. if (!empty($GLOBALS['_SESSION']['admin_id']) && $adminid)
  194. {

  195. }
  196. else
  197. {
  198. return false;
  199. }
  200. }

  201. function destroy_session()
  202. {
  203. $GLOBALS['_SESSION'] = array();

  204. setcookie($this->session_name, $this->session_id, 1, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);

  205. /* ECSHOP 自定义执行部分 */
  206. if (!empty($GLOBALS['ecs']))
  207. {
  208. $this->db->query('DELETE FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '$this->session_id'");
  209. }
  210. /* ECSHOP 自定义执行部分 */

  211. return $this->memcache->delete($this->session_id);
  212. }

  213. function get_session_id()
  214. {
  215. return $this->session_id;
  216. }

  217. function get_users_count()
  218. {
  219. $all_items = $this->db->getExtendedStats();

  220. return $all_items['127.0.0.1:11211']['curr_items'];//由于有其他key的缓存,因此这只是个接近数值

  221. }
  222. }

  223. ?>
复制代码


修改记录:
编辑函数改进

  1. function delete_spec_admin_session($adminid)
  2. {
  3. if (!empty($GLOBALS['_SESSION']['admin_id']) && $adminid)
  4. {
  5. //return $this->db->query('DELETE FROM ' . $this->session_table . " WHERE adminid = '$adminid'");
  6. $list = $mem->getExtendedStats('items');
  7. foreach($list['127.0.0.1:11211']['items'] as $k => $v){
  8. //echo $key = $k;echo "--";
  9. // echo $count = $v['number'];
  10. //echo "<br>";echo "<br>";echo "<br>";
  11. $ar = $mem-> getExtendedStats("cachedump", $key, $count);
  12. foreach($ar['127.0.0.1:11211'] as $kk => $vv){

  13. // echo $kk;
  14. //echo "<br>";
  15. $row = $mem->get($kk);
  16. if($row['adminid'] == $adminid){

  17. $mem->delete($kk);

  18. }

  19. }
  20. }
  21. }
  22. else
  23. {
  24. return false;
  25. }
  26. }
复制代码

下载测试。。

回答:
有问题一起反馈。我们一起完善。争取把ec的session完善

不错,帮顶下。

不错 支持一下

顶一下。

修改干掉管理员id下的memcache key
  1. function delete_spec_admin_session($adminid)
  2. {
  3. if (!empty($GLOBALS['_SESSION']['admin_id']) && $adminid)
  4. {
  5. //return $this->db->query('DELETE FROM ' . $this->session_table . " WHERE adminid = '$adminid'");
  6. $list = $mem->getExtendedStats('items');
  7. foreach($list['127.0.0.1:11211']['items'] as $k => $v){
  8. //echo $key = $k;echo "--";
  9. // echo $count = $v['number'];
  10. //echo "<br>";echo "<br>";echo "<br>";
  11. $ar = $mem-> getExtendedStats("cachedump", $key, $count);
  12. foreach($ar['127.0.0.1:11211'] as $kk => $vv){

  13. // echo $kk;
  14. //echo "<br>";
  15. $row = $mem->get($kk);
  16. if($row['adminid'] == $adminid){

  17. $mem->delete($kk);

  18. }

  19. }
  20. }
  21. }
  22. else
  23. {
  24. return false;
  25. }
  26. }
复制代码

我觉得要顶。。香港PHP空间www.51php.com

这个非常有用 支持一下