zoukankan      html  css  js  c++  java
  • 【discuzX2】/source/class/class_core.php文件中核心高效缓存类discuz_memory分析

    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.      * 确认当前系统支持的内存读写接口 
    20.      * @return discuz_memory 
    21.      */  
    22.     function discuz_memory() {//支持多种类型的缓存  
    23.         $this->extension['eaccelerator'] = function_exists('eaccelerator_get');  
    24.         $this->extension['apc'] = function_exists('apc_fetch');  
    25.         $this->extension['xcache'] = function_exists('xcache_get');  
    26.         $this->extension['memcache'] = extension_loaded('memcache');  
    27.     }  
    28.   
    29.         /** 
    30.      * 依据config当中设置,初始化内存引擎 
    31.      * @param unknown_type $config 
    32.      */  
    33.     function init($config) {  
    34.   
    35.         $this->config = $config;  
    36.         $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix'];  
    37.         $this->keys = array();  
    38.   
    39.         if($this->extension['memcache'] && !empty($config['memcache']['server'])) {  
    40.             require_once libfile('class/memcache');  
    41.             $this->memory = new discuz_memcache();  
    42.             $this->memory->init($this->config['memcache']);  
    43.             if(!$this->memory->enable) {  
    44.                 $this->memory = null;  
    45.             }  
    46.         }  
    47.   
    48.         if(!is_object($this->memory) && $this->extension['eaccelerator'] && $this->config['eaccelerator']) {  
    49.             require_once libfile('class/eaccelerator');  
    50.             $this->memory = new discuz_eaccelerator();  
    51.             $this->memory->init(null);  
    52.         }  
    53.   
    54.         if(!is_object($this->memory) && $this->extension['xcache'] && $this->config['xcache']) {  
    55.             require_once libfile('class/xcache');  
    56.             $this->memory = new discuz_xcache();  
    57.             $this->memory->init(null);  
    58.         }  
    59.   
    60.         if(!is_object($this->memory) && $this->extension['apc'] && $this->config['apc']) {  
    61.             require_once libfile('class/apc');  
    62.             $this->memory = new discuz_apc();  
    63.             $this->memory->init(null);  
    64.         }  
    65.   
    66.         if(is_object($this->memory)) {  
    67.             $this->enable = true;  
    68.             $this->type = str_replace('discuz_', '', get_class($this->memory));  
    69.             $this->keys = $this->get('memory_system_keys');  
    70.             $this->keys = !is_array($this->keys) ? array() : $this->keys;  
    71.         }  
    72.   
    73.     }  
    74.   
    75.         /** 
    76.      * 读取内存 
    77.      * 
    78.      * @param string $key 
    79.      * @return mix 
    80.      */  
    81.     function get($key) {  
    82.         $ret = null;  
    83.         if($this->enable) {  
    84.             $ret = $this->memory->get($this->_key($key));  
    85.             if(!is_array($ret)) {  
    86.                 $ret = null;  
    87.                 if(array_key_exists($key, $this->keys)) {  
    88.                     unset($this->keys[$key]);  
    89.                     $this->memory->set($this->_key('memory_system_keys'), array($this->keys));  
    90.                 }  
    91.             } else {  
    92.                 return $ret[0];  
    93.             }  
    94.         }  
    95.         return $ret;  
    96.     }  
    97.   
    98.         /** 
    99.      * 写入内存 
    100.      * 
    101.      * @param string $key 
    102.      * @param array_string_number $value 
    103.      * @param int过期时间 $ttl 
    104.      * @return boolean 
    105.      */  
    106.     function set($key, $value, $ttl = 0) {  
    107.   
    108.         $ret = null;  
    109.         if($this->enable) {  
    110.             $ret = $this->memory->set($this->_key($key), array($value), $ttl);  
    111.             if($ret) {  
    112.                 $this->keys[$key] = true;  
    113.                 $this->memory->set($this->_key('memory_system_keys'), array($this->keys));  
    114.             }  
    115.         }  
    116.         return $ret;  
    117.     }  
    118.   
    119.         /** 
    120.      * 删除一个内存单元 
    121.      * @param 键值string $key 
    122.      * @return boolean 
    123.      */  
    124.     function rm($key) {  
    125.         $ret = null;  
    126.         if($this->enable) {  
    127.             $ret = $this->memory->rm($this->_key($key));  
    128.             unset($this->keys[$key]);  
    129.             $this->memory->set($this->_key('memory_system_keys'), array($this->keys));  
    130.         }  
    131.         return $ret;  
    132.     }  
    133.   
    134.         /** 
    135.      * 清除当前使用的所有内存 
    136.      */  
    137.     function clear() {  
    138.         if($this->enable && is_array($this->keys)) {  
    139.             if(method_exists($this->memory, 'clear')) {  
    140.                 $this->memory->clear();  
    141.             } else {  
    142.                 $this->keys['memory_system_keys'] = true;  
    143.                 foreach ($this->keys as $k => $v) {  
    144.                     $this->memory->rm($this->_key($k));  
    145.                 }  
    146.             }  
    147.         }  
    148.         $this->keys = array();  
    149.         return true;  
    150.     }  
    151.   
    152.         /** 
    153.      * 内部函数 追加键值前缀 
    154.      * @param string $str 
    155.      * @return boolean 
    156.      */  
    157.     function _key($str) {  
    158.         return ($this->prefix).$str;  
    159.     }  
    160.   
    161. }  
    162. ?>  
  • 相关阅读:
    搭建james邮件服务器
    Spring -- AOP
    【RF库Collections测试】List Should Not Contain Duplicates
    【RF库Collections测试】Keep In Dictionary
    【RF库Collections测试】Insert Into List
    【RF库Collections测试】Get Index From List
    【RF库Collections测试】Get From List
    【RF库Collections测试】Count Values In List
    【RF库Collections测试】Get Slice From List
    【RF库Collections测试】Copy Dictionary
  • 原文地址:https://www.cnblogs.com/alleyonline/p/7498505.html
Copyright © 2011-2022 走看看