我为 PHP 安装了 memcache(没有 D)扩展。这是我用来创建 memcache 对象一次,然后重用它的函数。
# Memcache handler
class mem {
    var $id;
    var $expire;
    public static $mem;
    # Constructor
    function __construct($unique) {
        # ID
        $this->id = md5($unique);
        # Item expiration
        $this->expire = 86400;
        # Create or reuse object
        if(!isset(self::$mem)) {
            # Create new connection
            self::$mem = new Memcache;
            self::$mem->addServer('localhost', 11211);
        }
    }
    # Get
    public static function get() {
        # Check if object is cached
        if($data = self::$mem->get($this->id)) {
            # ID is cached
            return $data;
        } else {
            # ID is not cached
            return false;
        }
    }
    # Set
    public static function set($id, $data, $replace=false) {
        if(self::$mem->add($this->id, $data, MEMCACHE_COMPRESSED, $this->expire)) {
            # Write to memory
            return true;
        } elseif($replace) {
            # Replace information
            self::$mem->replace($this->id, $merge, MEMCACHE_COMPRESSED, $this->expire);
            return true;
        } else {
            # Return nothing
            return false;
        }
    }
    # Delete key
    public static function delete() {
        if(self::$mem->delete($this->id)) {
            return true;
        } else {
            return false;
        }
    }
}
使用示例
用户将数据写入内存:
mem::set('some_clever_ID', array('this','and','that'));
用户从内存中获取数据:
$data = mem::get('some_clever_ID');
用户打印抓取的数据:
print_r($data); //should print an array with ['this', 'and', 'that']
用户从内存中删除密钥:
mem::delete('some_clever_ID');
我有两个问题:
为什么我会收到此错误?致命错误:在 /inc/memcache.php 中的非对象上调用成员函数 add()
我想知道是否有任何方法可以提高此代码的性能,或者我是否应该实施不同的方法。这个想法是快速管理 memcache 值并在可能的情况下重用 memcache 连接。
任何帮助和评论将不胜感激。
更新:解决方案
我最终将 memcache 函数放在了课堂之外。
# Memcache settings
$config['memcache']['servers'][] = array('localhost',11211);
$config['memcache']['debug'] = true;
# Create memcached object
function cache() {
    global $config;
    static $cache;
    if(!isset($cache)) {
        $cache = new Memcache;
        foreach($config['memcache']['servers'] as $server) {
            $cache->addServer($server[0], $server[1]);
        }
    }
    return $cache;
}
# Memcache handler
class mem {
    var $id;
    var $expire;
    # Constructor
    function __construct($unique) {
        global $config;
        # ID
        $this->id = md5(PRO_NAME.'_'.$unique);
        # Item expiration
        if($config['memcache']['debug']) {
            $this->expire = 5;
        } else {
            $this->expire = 86400;
        }
    }
    # Get
    function get() {
        # Check if object is cached
        if($data = cache()->get($this->id)) {
            # ID is cached
            return $data;
        } else {
            # ID is not cached
            return false;
        }
    }
    # Set
    function set($data, $replace=false) {
        if(cache()->add($this->id, $data, MEMCACHE_COMPRESSED, $this->expire)) {
            # Write to memory
            return true;
        } elseif($replace) {
            # Replace information
            if(cache()->replace($this->id, $data, MEMCACHE_COMPRESSED, $this->expire)) {
                return true;
            } else {
                return false;
            }
        } else {
            # Return nothing
            return false;
        }
    }
    # Delete key
    function delete() {
        if(cache()->delete($this->id)) {
            return true;
        } else {
            return false;
        }
    }
}
写例子
$obj = new mem('clever_ID_here');
$obj->set('some data');
//or use $obj->set('some data', true); if you want to replace previous value
举个例子
$obj = new mem('clever_ID_here');
echo $obj->get();
删除示例
$obj = new mem('clever_ID_here');
$obj->delete(); //if you wish to delete the key