【PHPLIb】Cache操作封装类——Memcache/APC

<?php
interface Project_Cache_Interface{
    public function set($key, $value, $expire=0);
    public function get($key);
    public function del($key);
    public function inc($key, $step=1);
    public function dec($key, $step=1);
    public function clear();
    public function stats();
}

class Project_Cache_Memcache implements Project_Cache_Interface {
    public $_mObject = null;

    /*
     * Separator
     */
    const CST_SPLIT_OR = '||';
    const CST_SPLIT_COLON = ':';

    /* {{{ Method Constructor()
     *
     * Constructor, Init Project_Cache object
     * @param string $config    Config string,
     *                          e.g.192.168.199.69:11211||192.168.199.69:11212
     * @return  void
     */
    public function __construct($config = '') {
        if (!extension_loaded('memcache')) {
            throw new Exception('Do not have memcache support');
        }

        // Get server variable: MEMCACHE_SERVER
        // from Web server config file.
        if(empty($config)){
            $config = @$_SERVER['MEMCACHE_SERVER'];
        }

        // Format checking
        if(!preg_match('/^\d+(?:\.\d+){3}:\d+(?:\|\|\d+(?:\.\d+){3}:\d+)*$/', $config)){
            throw new Exception('The config is wrong');
        }

        // Init Memcache and add servers
        $this->_mObject = new Memcache;
        foreach(explode(self::CST_SPLIT_OR, $config) as $host){
            $arr = explode(self::CST_SPLIT_COLON, $host);
            $this->_mObject->addServer(
                $arr[0], /* ip */
                $arr[1] /* port */
            );
        }
    }
    /* }}} */

    public function __destruct() {
        $this->_mObject->close();
    }

    public function set($key, $value, $expire=0) {
        return $this->_mObject->add(
            $key, $value, false, $expire
        );
    }

    public function get($key) {
        return $this->_mObject->get($key);
    }

    public function del($key) {
        return $this->_mObject->delete($key);
    }

    public function inc($key, $step=1) {
        return $this->_mObject->increment($key, $step);
    }

    public function dec($key, $step=1) {
        return $this->_mObject->decrement($key, $step);
    }

    public function clear() {
        return $this->_mObject->flush();
    }

    public function stats() {
        return $this->_mObject->getStats();
    }
}

class Project_Cache_Apc implements Project_Cache_Interface {
    public function __construct($config = '') {
        if(!function_exists('apc_cache_info')) {
            throw new Exception('Do not have APC support');
        }
        //todo : $config
    }

    public function __destruct() {
        //todo:
    }

    public function set($key, $value, $expire=0) {
        return apc_add($key, $value, $expire);
    }

    public function get($key) {
        return apc_fetch($key);
    }

    public function del($key) {
        return apc_delete($key);
    }

    public function inc($key, $step=1) {
        return apc_inc($key, $step);
    }

    public function dec($key, $step=1) {
        return apc_dec($key, $step=1);
    }

    public function clear() {
        // If cache_type is "user", the user cache will be cleared; otherwise,
        // the system cache (cached files) will be cleared.
        return apc_clear_cache('user');
    }

    public function stats() {
        // 1. If cache_type is "user", information about the user cache will be returned.
        // 2. If cache_type is "filehits", information about which files have been served
        //    from the bytecode cache for the current request will be returned. This feature
        //    must be enabled at compile time using --enable-filehits .
        // 3. If an invalid or no cache_type is specified, information about the system
        //    cache (cached files) will be returned.
        return apc_cache_info('user');
    }
}

/**
 * Cache操作封装类
 *  $Cache = new Cache(Cache::CST_MEMCACHE);
 *  $Cache = $Cache->getInstance();
 *  $Cache->set('key', 'hello world');
 *  print_r($Cache->get('key'));
 *  print_r($Cache->stats());
 */

class Project_Cache {
    // Constants
    const CST_APC = 'apc';              // For APC
    const CST_MEMCACHE = 'memcache';    // For Memcache

    private $_mType = 'memcache';       // Cache engine
    private $_mConfig = '';             // Config string
                                        // eg. 127.0.0.1:11211||127.0.0.1:11212||127.0.0.1:11213||127.0.0.1:11214

    /** 
     * {{{ Method Constructor()
     *
     * Constructor
     * @param int $type    Cache engine :
     *                     1. Project_Cache:CST_MEMCACHE
     *                     2. Project_Cache:CST_APC
     * @param string $config    Config string // For Memcache
     * @return  void
     */
    public function __construct($type = self::CST_APC, $config = '')
    {
        $this->_mType = $type;
        $this->_mConfig = $config;
    }
    /* }}} */

    /** 
     * {{{ Method getInstance()
     * Object Factory
     * @return object
     */
    public function getInstance()
    {
        $cacheType = 'Project_Cache_' . $this->_mType;
        try {
            return new $cacheType($this->_mConfig);
        } catch(Exception $e) {
            throw new Exception('Cache configuration type is error!');
        }
    }
    /* }}} */

    /** 
     * {{{ Method Destructor()
     *
     */
    public function __destruct()
    {
        //todo:
    }
    /* }}} */
}

$Cache = new Project_Cache(Project_Cache::CST_MEMCACHE, "127.0.0.1:11211||127.0.0.1:11212||127.0.0.1:11213||127.0.0.1:11214");
$Cache = $Cache->getInstance();
$Cache->set('key', 'hello world');
print_r($Cache->get('key'));
print_r($Cache->stats());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值