开发者

Undefined property: Add::$cache - Codeigniter

I'm experiencing a serious problem when I'm trying to use the Memcached caching driver in Codeigniter. However, I cannot autoload the cache driver in this array:

$autoload['libraries'] = array('database', 'session', 'parser', 'common', 'cache');

If I remove the cache, the page loads, but if not the page shows blank with no error reporting (note, I have error reporting on and set to E_ALL).

Anyway, since I cannot load the cache, I added it to my created class common, and load it from there doing this:

class Common extends CI_Controller {

    function __construct()
    {
        parent::__construct();

        $this->load->driver('cache');
    }
}

Now I'm getting in errors:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Add::$cache

Filename: controllers/add.php

Line Number: 33 A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/add.php

Line Number: 33

Which add.php is this:

class Add extends CI_Controller {

    public function index()
    {
        $data = array('page_title' => 'Add your listing');

        $this->load->view('overall_header', $data);

        $sql = "SELECT item_id,
                       item_name
                FROM " . ITEMS_TABLE . "
                WHERE item_visible = 1";

        $items_key = md5('query:' . $sql);

        $result = $this->cache->memcached->save($items_key); // this is line 33
    }
}

I need memcached to work for my Codeigniter application, I've tried using this custom made class from github.

How can I get memcached to work, I have all the settings in the application/config/memcached.php properly set, but I guess that's not 开发者_JAVA技巧the problem.


First of all make sure PHP has the memcached library loaded. PHP uses two type of library: memcache and memcached (note the D at the end). Codeigniter cache driver needs the memcached extension.

This driver is quite buggy. First, the default options are not loaded correctly so you need to make a config/memcached.php file

This is how my config/memcached.php looks:

<?php
$config['memcached'] = array(
        'hostname'  => '127.0.0.1',
        'port'      => 11211,
        'weight'    => 1
);

In my controller/model I load the driver in this way:

$this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'dummy'));

So if memcached is not available, a dummy driver is loaded as fallback.

Take care that the current stable release of CodeIgniter (2.0.2) has another bug in the driver loader class. Here the patch I applied

After this fixes I got the driver working

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜