如何在CodeIgniter中清除页面缓存

8

我使用CodeIgniter。我的页面中一部分总是被缓存,无法通过浏览器的Ctrl+F5清除。当我在视图中更改页面名称时,它却起作用了!?

如何在CodeIgniter中清除页面缓存?

5个回答

10

https://www.codeigniter.com/user_guide/general/caching.html 是当前3.x的链接... - Gert van den Berg

4
function delete_cache($uri_string=null)
{
    $CI =& get_instance();
    $path = $CI->config->item('cache_path');
    $path = rtrim($path, DIRECTORY_SEPARATOR);

    $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

    $uri =  $CI->config->item('base_url').
            $CI->config->item('index_page').
            $uri_string;

    $cache_path .= md5($uri);

    return unlink($cache_path);
}

这个答案缺少了它的教育性解释。 - undefined

4
public function clear_path_cache($uri)
{
    $CI =& get_instance();
    $path = $CI->config->item('cache_path');
    //path of cache directory
    $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

    $uri =  $CI->config->item('base_url').
    $CI->config->item('index_page').
    $uri;
    $cache_path .= md5($uri);

    return @unlink($cache_path);
}




/**
 * Clears all cache from the cache directory
 */
public function clear_all_cache()
{
    $CI =& get_instance();
    $path = $CI->config->item('cache_path');

    $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

    $handle = opendir($cache_path);
    while (($file = readdir($handle))!== FALSE) 
    {
        //Leave the directory protection alone
        if ($file != '.htaccess' && $file != 'index.html')
        {
           @unlink($cache_path.'/'.$file);
        }
    }
    closedir($handle);       
}

这个答案缺少了教育性的解释。 - undefined

0

Codeigniter 3

我在控制器中添加了一个端点

function delete_cache() {
    preg_match("/(\/index.php)(?<endpoint>\/[\w.-\/]*)/", 
    $_SERVER["HTTP_REFERER"], $result);
    $this->output->delete_cache($result["endpoint"]);
    redirect($result["endpoint"]);
}

然后我添加了一个指向此端点的刷新按钮。 当用户单击刷新按钮时,当前缓存文件将被删除,然后重定向到页面。


那个正则表达式模式真的是想要包括从一个字面上的句点到一个斜杠的字符范围吗? - undefined

-6

这可能是在一个session中。尝试删除你的cookies。


我在谷歌浏览器中删除了Cookie,但不起作用。 - Jennifer Anthony
1
它保存在服务器端,如此描述:http://codeigniter.com/user_guide/general/caching.html - nonchip

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接