CodeIgniter Forums

Full Version: Your opinion on file helper performance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Référencement Google[/eluser]
Hi,

I would like to know you opinion about the performances of that piece of code:

Code:
function random_head_image()
    {
        $this->load->helper('file');
        $this->load->helper('array');

        $head_image = get_filenames('public/images/head');
        return random_element($head_image);
    }

To explain what it does: It reads a whole directory then returns a random file name.

I am worrying about performances because there will be about 30 files inside the directory (each file is about 50-60 Ko) and the function will be called on each page load to display a random picture.

Is this a correct way to do it or will it make pain to the server on high traffic hours? What are your thought on that?

El Forum

[eluser]Derek Jones[/eluser]
If it's just 30 files, probably not too bad, but with many files, and if you get tremendous traffic spikes, you will certainly be causing the disk i/o to go through the roof. I'd recommend some sort of caching system for reading the directory, perhaps writing the serialized array to a cache file, and only updating it every hour, day, week, or whatever your specific needs are for keeping it dynamic. That way for most requests, you have only one file read, and no directories traversed with readdir().

El Forum

[eluser]Référencement Google[/eluser]
Thank you Derek, of course I am stupid not to have think about caching, specially when the files won't change for maybe 6 months !

That said, that's also where the CI caching lib could be improved, it's not enough flexible to do custom things.

El Forum

[eluser]Derek Allard[/eluser]
If it won't be changing for 6 months, and performance is a concern, consider skipping the file helper altogether and just manually construct it.

El Forum

[eluser]Référencement Google[/eluser]
Yes, that's what I was planing to do after thought about it, thanks both of you.