繁体   English   中英

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

[英]How can I access cache from another controller in Codeigniter?

$ 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);

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

更新:
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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM