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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (10)

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

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>';
    }
    ?>

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

提问于
用户回答回答于

您可以通过使用将缓存数据传递给查看 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
    }
用户回答回答于
$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

扫码关注云+社区

领取腾讯云代金券