簡體   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