設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 919|回復: 0
打印 上一主題 下一主題

【discuzX2】/source/class/class_core.php discuz_memory分析

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2015-2-8 23:29:15 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. <?php
  2. /**
  3. * Discuz 内存读写引擎
  4. * 支持 memcache, eAccelerator, XCache
  5. *
  6. * 使用的时候建议直接利用函数 memory()
  7. */
  8. class discuz_memory
  9. {
  10.         var $config;
  11.         var $extension = array();
  12.         var $memory;
  13.         var $prefix;
  14.         var $type;
  15.         var $keys;
  16.         var $enable = false;

  17.         /**
  18.          * 确认当前系统支持的内存读写接口
  19.          * @return discuz_memory
  20.          */
  21.         function discuz_memory() {//支持多种类型的缓存
  22.                 $this->extension['eaccelerator'] = function_exists('eaccelerator_get');
  23.                 $this->extension['apc'] = function_exists('apc_fetch');
  24.                 $this->extension['xcache'] = function_exists('xcache_get');
  25.                 $this->extension['memcache'] = extension_loaded('memcache');
  26.         }

  27.         /**
  28.          * 依据config当中设置,初始化内存引擎
  29.          * @param unknown_type $config
  30.          */
  31.         function init($config) {

  32.                 $this->config = $config;
  33.                 $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix'];
  34.                 $this->keys = array();

  35.                 if($this->extension['memcache'] && !empty($config['memcache']['server'])) {
  36.                         require_once libfile('class/memcache');
  37.                         $this->memory = new discuz_memcache();
  38.                         $this->memory->init($this->config['memcache']);
  39.                         if(!$this->memory->enable) {
  40.                                 $this->memory = null;
  41.                         }
  42.                 }

  43.                 if(!is_object($this->memory) && $this->extension['eaccelerator'] && $this->config['eaccelerator']) {
  44.                         require_once libfile('class/eaccelerator');
  45.                         $this->memory = new discuz_eaccelerator();
  46.                         $this->memory->init(null);
  47.                 }

  48.                 if(!is_object($this->memory) && $this->extension['xcache'] && $this->config['xcache']) {
  49.                         require_once libfile('class/xcache');
  50.                         $this->memory = new discuz_xcache();
  51.                         $this->memory->init(null);
  52.                 }

  53.                 if(!is_object($this->memory) && $this->extension['apc'] && $this->config['apc']) {
  54.                         require_once libfile('class/apc');
  55.                         $this->memory = new discuz_apc();
  56.                         $this->memory->init(null);
  57.                 }

  58.                 if(is_object($this->memory)) {
  59.                         $this->enable = true;
  60.                         $this->type = str_replace('discuz_', '', get_class($this->memory));
  61.                         $this->keys = $this->get('memory_system_keys');
  62.                         $this->keys = !is_array($this->keys) ? array() : $this->keys;
  63.                 }

  64.         }

  65.         /**
  66.          * 读取内存
  67.          *
  68.          * @param string $key
  69.          * @return mix
  70.          */
  71.         function get($key) {
  72.                 $ret = null;
  73.                 if($this->enable) {
  74.                         $ret = $this->memory->get($this->_key($key));
  75.                         if(!is_array($ret)) {
  76.                                 $ret = null;
  77.                                 if(array_key_exists($key, $this->keys)) {
  78.                                         unset($this->keys[$key]);
  79.                                         $this->memory->set($this->_key('memory_system_keys'), array($this->keys));
  80.                                 }
  81.                         } else {
  82.                                 return $ret[0];
  83.                         }
  84.                 }
  85.                 return $ret;
  86.         }

  87.         /**
  88.          * 写入内存
  89.          *
  90.          * @param string $key
  91.          * @param array_string_number $value
  92.          * @param int过期时间 $ttl
  93.          * @return boolean
  94.          */
  95.         function set($key, $value, $ttl = 0) {

  96.                 $ret = null;
  97.                 if($this->enable) {
  98.                         $ret = $this->memory->set($this->_key($key), array($value), $ttl);
  99.                         if($ret) {
  100.                                 $this->keys[$key] = true;
  101.                                 $this->memory->set($this->_key('memory_system_keys'), array($this->keys));
  102.                         }
  103.                 }
  104.                 return $ret;
  105.         }

  106.         /**
  107.          * 删除一个内存单元
  108.          * @param 键值string $key
  109.          * @return boolean
  110.          */
  111.         function rm($key) {
  112.                 $ret = null;
  113.                 if($this->enable) {
  114.                         $ret = $this->memory->rm($this->_key($key));
  115.                         unset($this->keys[$key]);
  116.                         $this->memory->set($this->_key('memory_system_keys'), array($this->keys));
  117.                 }
  118.                 return $ret;
  119.         }

  120.         /**
  121.          * 清除当前使用的所有内存
  122.          */
  123.         function clear() {
  124.                 if($this->enable && is_array($this->keys)) {
  125.                         if(method_exists($this->memory, 'clear')) {
  126.                                 $this->memory->clear();
  127.                         } else {
  128.                                 $this->keys['memory_system_keys'] = true;
  129.                                 foreach ($this->keys as $k => $v) {
  130.                                         $this->memory->rm($this->_key($k));
  131.                                 }
  132.                         }
  133.                 }
  134.                 $this->keys = array();
  135.                 return true;
  136.         }

  137.         /**
  138.          * 内部函数 追加键值前缀
  139.          * @param string $str
  140.          * @return boolean
  141.          */
  142.         function _key($str) {
  143.                 return ($this->prefix).$str;
  144.         }

  145. }
  146. ?>
複製代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-15 14:40 , Processed in 0.267041 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表