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

时间:2016-04-07 15:01:54

标签: php codeigniter controller

$ data = array('Product'=>'test','Price'=> 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);

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

1 个答案:

答案 0 :(得分:0)

<强>更新
CI包含memcached驱动程序而不是memcache,所以如果你想使用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);
    }
}