如何在php中访问memcached的对象

时间:2016-01-07 12:01:52

标签: php codeigniter amazon-web-services memcached

我需要什么

  • 我需要在ci。
  • 中访问memcahe的对象

代码段

class Cache_model extends CI_Model {

public function __construct() 
{
            parent::__construct ();
            $this->load->model('memcache_model'); 
            $m = new Memcached();
             $m->addServer('localhost', 11211);
        $this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'file'));
}

     // function that works

     public function getCatLevelInfo($cat_id, $cat_index)
{
    $Data = array();
  $m = new Memcached();
 $m->addServer('localhost', 11211);
 $cat_info = $m->get($cat_index.$cat_id);
}

 }


  // function that don"t works



public function getCatLevelInfo($cat_id, $cat_index)
{
    $Data = array();

 $cat_info = $this->get($cat_index.$cat_id);
}
  • 我想访问在构造函数中初始化的$ m对象。
  • 我尝试使用此功能但访问500x htp repsonse代码。
  • 欢迎提出任何建议。
  • 打印对象$ m响应为1。

1 个答案:

答案 0 :(得分:2)

您需要将$m声明为全局变种

<?php
/**
 * Created by PhpStorm.
 * User: Ilan Hasanov
 * Date: 1/7/2016
 * Time: 1:02 PM
 */
class Cache_model extends CI_Model {

    private $m = null;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('memcache_model');
        $this->m = new Memcached();
        $this->m->addServer('localhost', 11211);
        $this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'file'));
    }

    // function that works

    public function getCatLevelInfo($cat_id, $cat_index)
    {
        $cat_info = $this->m->get($cat_index.$cat_id);
        var_dump($cat_info);
    }
}