0

我为 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');

我有两个问题:

  1. 为什么我会收到此错误?致命错误:在 /inc/memcache.php 中的非对象上调用成员函数 add()

  2. 我想知道是否有任何方法可以提高此代码的性能,或者我是否应该实施不同的方法。这个想法是快速管理 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
4

2 回答 2

3

$mem is a static property on your class so instead of accessing it as $mem

you should use self::$mem;

From a class design standpoint you should not be using global in the constructor for the class , you should instead pass a reference of the config object into the class. For example:

 function __construct($unique, $config) {

Since all of the methods are being called statically there is a good chance the constructor is never being called.

To get arround this you can create a protected static method to serve you up with an instance of the memcached

protected static function getMemcache() {

    # Create or reuse object
    if(!isset(self::$mem)) {
        # Create new connection
        self::$mem = new Memcache();
        self::$mem->addServer('localhost', 11211);
    }
    return self::$mem;
}

Everywhere you currently access self::$mem replace it with self::getMemcache()

于 2013-06-10T17:07:46.267 回答
3

In the functions of your class, you have to do $this->mem instead of $mem.

I suggest you checking this class: PHPFastCache, which is very simple and allows you to use not only Memcache(d), but another caching systems as well, without changing anything in your code.

于 2013-06-10T17:09:18.387 回答