如何访问 php 中的 memcached 对象
how to acces object of memcached in php
我需要什么
- 我需要访问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 响应代码。
- 欢迎提出任何建议。
- 打印对象 $m 响应为 1 。
您需要将 $m
声明为全局变量 try
<?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);
}
}
我需要什么
- 我需要访问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 响应代码。
- 欢迎提出任何建议。
- 打印对象 $m 响应为 1 。
您需要将 $m
声明为全局变量 try
<?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);
}
}