Php 如何在CodeIgniter中使用多个缓存文件夹
我有一个多语言网站,我想为每种语言创建一个特定的缓存文件夹。我该怎么做? 我目前正在使用一个缓存文件夹与此代码 你能帮我吗?Php 如何在CodeIgniter中使用多个缓存文件夹,php,codeigniter,caching,codeigniter-3,Php,Codeigniter,Caching,Codeigniter 3,我有一个多语言网站,我想为每种语言创建一个特定的缓存文件夹。我该怎么做? 我目前正在使用一个缓存文件夹与此代码 你能帮我吗? $lang = $CI->session->userdata('language'); $cache_path .= md5($uri).'-'.$lang; 配置缓存文件夹的参数位于config.php中,其名为cache\u path。为了在运行时修改,我们可以使用$this->config->set_item函数。显然,在调用缓存函数之前,应该在控制器
$lang = $CI->session->userdata('language');
$cache_path .= md5($uri).'-'.$lang;
配置缓存文件夹的参数位于
config.php
中,其名为cache\u path
。为了在运行时修改,我们可以使用$this->config->set_item
函数。显然,在调用缓存函数之前,应该在控制器函数中尽早完成缓存文件夹切换
下面是一个示例实现,controllerTest
:
<?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');
}
}
希望它能帮助我找到解决办法。我们必须像这样在核心文件夹中编辑
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
}
为什么需要多个文件夹?如果用户想要更改网站的语言,则缓存会映射到用户会话,因此一个可以正常工作。需要删除所有缓存,然后更改语言。此外,我的网站是双向RTL和LTR。所以我需要为缓存创建一个文件夹,并防止在语言更改时删除缓存。
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
}