Welcome Guest, Not a member yet? Register   Sign In
Too many file in cache dir: solution
#1

[eluser]Chalda Pnuzig[/eluser]
Hi all,
I had this problem:
In the cache I had more than 10.000 files, and that probably influence the performance of reading.
To resolve that I added these lines of code.
That split cache files in multiple subdirectory.

In system/libraries/output.php - function _write_cache
Code:
$uri =    $CI->config->item('base_url').
                $CI->config->item('index_page').
                $CI->uri->uri_string();

        // Chalda here: cache in subdirectory
        $md5 =  md5($uri);
        $cache_path.= substr($md5, 0,1).'/';
        if ( ! is_dir($cache_path)){
            mkdir($cache_path);
        }
        $cache_path.=$md5;
        // end cache in subdirectory

        if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
        {
            log_message('error', "Unable to write cache file: ".$cache_path);
            return;
        }

In system/libraries/output.php - function _display_cache
Code:
if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
        {
            return FALSE;
        }

        // Chalda here: cache in subdirectory
        $md5 = md5($uri);
        $cache_path.= substr($md5, 0,1).'/';
        if ( ! is_dir($cache_path)){
            return FALSE;
        }
        // end cache in subdirectory

        $filepath = $cache_path.md5($uri);



It divides the cache into subfolders according to the first character of the md5's file


Example:
cache before the change
Code:
cachedir/007dcd4f736e9be08adbe71f15e87bd9
cachedir/aee5f9543140bee532d3fe5e9b990e38
cachedir/a8cc3229af15a6259c33a5302884409f
cachedir/c5615a9a01e68d34682b2f7185bf5437

cache after the change
Code:
cachedir/0/007dcd4f736e9be08adbe71f15e87bd9
cachedir/a/aee5f9543140bee532d3fe5e9b990e38
cachedir/a/a8cc3229af15a6259c33a5302884409f
cachedir/c/c5615a9a01e68d34682b2f7185bf5437




It's ok?
Do you have any other suggestions?
#2

[eluser]abedzilla[/eluser]
This is very nice. It could solve the problems total file limitation for some filesystem in a single directory.


I've created MY_Ouput.php and added your code into it and overwrite the _write_cache() and _display_cache() method, so it doesn't break the Output.php core class.




Theme © iAndrew 2016 - Forum software by © MyBB