如何从Codeigniter中的另一个控制器访问缓存


How can I access cache from another controller in Codeigniter?

$data=array('Product'=>'test','Rice'=>150.23,'return_url'=>'111');

    $transaction_id = GUID();
    $cache = new Memcache();
    $cache->addserver('127.0.0.1', 11211, 3);
    $cache->set($transaction_id, $data, MEMCACHE_COMPRESSED, 60);

我需要从另一个控制器的函数中的数组中调用此数据。

更新:
CI包含不用于memcache的memcached驱动程序,所以如果您想使用memcache,请尝试以下操作:

// to define the cache key in application/config/constants.php
define('TRANSACTION_CACHE_KEY', 'transaction_cache_key');
//application/core/My_Controller.php
class My_Controller extends CI_Controller {
    protected $cache = null;
    public function __construct() {
        parent::__construct();
        $this->cache = new Memcache();
        $this->cache->addserver('127.0.0.1', 11211, 3);
    }
}
controller1 extends My_Controller {
    protected function getTransactionData() {
        return array('Product' => 'test','Price' => 150.23,'return_url' =>'111');
    }
    public function setTransaction() {
        $data = $this->getTransactionData();
        $this->cache->set(TRANSACTION_CACHE_KEY, $data, MEMCACHE_COMPRESSED, 60);
    }
}
controller2 extends My_Controller {
    public function getTransaction() {
        $data = $this->cache->get(TRANSACTION_CACHE_KEY);
    }
}