如何在CodeIgniter中使用多个缓存文件夹

时间:2018-06-09 12:25:05

标签: php codeigniter caching codeigniter-3

我有一个多语言网站我想为每种语言创建一个特定的缓存文件夹。我怎样才能做到这一点? 我目前使用一个带有此代码的缓存文件夹。

你能帮助我吗?

        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Content-Description: File Transfer');
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename={$name}");
        header("Expires: 0");
        header("Pragma: public");

2 个答案:

答案 0 :(得分:0)

配置缓存文件夹的参数位于config.php,其名称为cache_path。要在运行时修改,我们可以使用$this->config->set_item函数。显然,缓存文件夹切换应该在缓存函数调用之前尽早在控制器函数中完成。

以下是一个示例实现,控制器Test

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $user_language = 'french';

        $this->config->set_item('cache_path', 'IS_ROOT/cache/' . $user_language . '/');

        $this->output->cache(1);

        $this->load->view('welcome_message');
    }
}

当你运行它时,你可以在日志中看到目录&#34; french&#34;在&#34;全球&#34;使用了IS_ROOT / cache:

INFO - 2018-06-09 22:12:39 --> File loaded: /php_basedir/CodeIgniter_3_1_8/application/views/welcome_message.php
DEBUG - 2018-06-09 22:12:39 --> Cache file written: IS_ROOT/cache/french/bc3ad60292ed776397da07cac67ddd28
INFO - 2018-06-09 22:12:39 --> Final output sent to browser
DEBUG - 2018-06-09 22:12:39 --> Total execution time: 0.0138

希望有所帮助

答案 1 :(得分:0)

我找到了解决方案。我们必须像这样编辑核心文件夹中的output.php

        public function _write_cache($output){
            $CI =& get_instance();
            $lang = $CI->session->userdata('language');
            $path = $CI->config->item('cache_path');
            $cache_path = ($path === '') ? APPPATH.'cache_'.$lang.'/' : $path;
            //other code
        }
        public function _display_cache(&$CFG, &$URI){
            $CI =& get_instance();
            $lang = $CI->session->userdata('language');
            $cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache_'.$lang.'/' : $CFG->item('cache_path');
            //other code
        }