tags:

views:

55

answers:

3

I have a text file that is around 1.7MB that has a list of words that I need for my application. I need to have it loaded into an array when the page loads and then be available to other PHP files that AJAX requests are made to. Right now I am loading it into an array on page load and then storing it in a session so that the AJAX file has it available when requests are made to it. Is this the best way to do this?

+4  A: 

Really depends on what you are doing with the data. If the AJAX only needs a certain couple of words from that huge list it would probably be best stored in a database.

Do remember that even if the list is stored in a session it will imply that you script loads up that array on every single call. Seams like a huge burden for something that maybe is not really needed.

You don't give us much detail, though.

Frankie
and you could gain time performing sql queries instead of loopin the entire 1.7MB of words
pleasedontbelong
Just for some more clarification, the array would have close to 200,000 elements (words). The AJAX request is basically checking if the given word is in the array.
Brandon
@Brandon don't hesitate in putting it into a DB. You'll get a boost in performance (speed search) using decreased resources (less ram).
Frankie
A: 

If the entire array must be loaded into PHP, I'd write a script that made it into a PHP script that contains an array definition of the list of words, so you'd have a .php file that contains:

$myArray = array('blah', 'blah', 'blah'); //etc

Then, I'd include that (require_once)... most PHP script engines will be able to optimize that pretty easily... not much point in storing it in a session, would just use up more disk space/DB space/memory for storage.

DulabCMS
A: 

The file command is pretty quick, so I'm sure there's going to be much to gain by caching anything.

Do you use the data on every single page load? If not, don't go with sessions as you'll have the overhead of loading it even when you don't need it.

Does the data depend upon the user? If not, don't go with session as you'll be duplicating the data all over the place for no reason.

If you are doing more complex parsing, then I'd suggest caching the file using

file_put_contents($filename, '<?php return '.var_export($array, true).';');

Then, all you need to do is $data = include $filename to fetch the data...

You could even make it smart enough to detect changes:

function getData() {
    static $data = array();
    if (empty($data)) {
        $cache = '/path/to/cache/file';
        $full = '/path/to/original/file';
        if (!file_exists($cache) || filemtime($cache) < filemtime($full)) {
            $data = parseDataFileIntoArray($full);
            $code = '<?php return '.var_export($data, true).';';
            file_put_contents($cache, $code);
        } else {
            $data = include $cache;
        }
    }
    return $data;
}
ircmaxell
Care to explain the downvote? Assuming that the entire array must be in PHP land (not using a DB), this is a fairly decent design. It avoids the double memory usage issue on each page load that anything serialized would have. It's usable on all servers (since it doesn't require a PECL extension to work). And it will take full advantage of an OPCode cache if you have one installed (With no need to change the function)...
ircmaxell