代码点火器从缓存输出数据

时间:2015-05-11 11:28:00

标签: php caching codeigniter-2

我的控制器中有以下功能:

public function cached_icd10() {
        /*
Get information from database and cache the information         */
        $this->load->driver('cache', array('adapter' => 'file'));

        $cacheID = "icd10";
        if (!$cache_data = $this->cache->get($cacheID)) {
            //ge information from the  database 
            $data['icd10_codes'] = $this->icd_10_codes();

            // Save into the cache for 5 minutes
            $this->cache->save($cacheID, $data['icd10_codes'], 300);
            $cache_data = $data['icd10_codes'];
        }
        return $cache_data;

    }

    public function form() {
        //Fetch all ICD10 Codes from the cache file which is icd10

        $cache_data = $this->cached_icd10();


        $this->load->view('form', $cache_data);
    }

第一个函数缓存来自数据库的信息,第二个函数将其传递给名为form的视图。当我尝试从我的视图输出信息时,它无法抛出错误:消息:未定义的变量:cache_data,而当我尝试从控制器执行此操作时,它很好地回应它。使用以下代码:

 <?php
    foreach ($cache_data as $value) {
        echo 'Out put per line is ....:    ' . $value['icd_description'] . ' and the  Id is .... ' . $value['id'] . '<br>';
    }
    ?>

如何从控制器向我的视图显示此信息?

2 个答案:

答案 0 :(得分:1)

您可以使用array

将缓存数据传递给查看
public function form() {
     //Fetch all ICD10 Codes from the cache file which is icd10
        $cache_data = $this->cached_icd10();
        $data['cache_data']=$cache_data;// create and pass data to array
        $this->load->view('form', $data);
    }

您可以使用

在视图中缓存数据
foreach ($cache_data as $value) {
       // your code here
    }

答案 1 :(得分:0)

$this->load->driver('cache',
        array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => 'my_')
);

$this->cache->get('foo'); // Will get the cache entry named 'my_foo'

引用网址:https://www.codeigniter.com/userguide3/libraries/caching.html#example-usage