PHP Cache Mechanism

Omid Navy asked
php mysql caching hashtable apcu

I was working on a program which needs a cache system. so the description is I got a mysql db which has 4 columns, ‘mac’,’src’,’username’,’main’ Which mac,src,username are key/values and foreign key in main table. it will insert to those 3 first and put their ID in main. The data I got is about 18m for main table, and for those 3 about 2m each. I dont want to use a select everytime it needs to insert in main, so I used an array to cache them. $hash= [‘mac’=>[],’src’=>[],’username’=>[]]; and store ‘n fetch data like this : $hash[‘mac’][‘54:52:00:27:e4:91’];

This approach got bad performance when the hash data’s goo beyond 500k ; So is there any better way to do this ?

PS: I got same thing with nodeJS which i used a npm module named hashtable And Performance was about 10k inserts each 4m. I’ve read about php arrays and found out they are Hashtables , but now it do the same job with far wayslower, for only 1k it takes atleast 5minutes;


Assuming you’re on a Linux server. See: Creating a RAM disk. Once you have a RAM disk, cache each ID as a file, using a sha1() hash of the mac address. The RAM disk file is, well, RAM; i.e., a persistent cache in memory.

$mac   = '54:52:00:27:e4:91';
$cache = '/path/to/ramdisk/'.sha1($mac);

if (is_file($cache)) { // Cached already?
    $ID = file_get_contents($cache); // From the cache.
} else {
    // Run SQL query here and get the $ID.

    // Now cache the $ID.
    file_put_contents($cache, $ID); // Cache it.
// Now do your insert here.

To clarify: A RAM disk allows you to use filesystem wrappers in PHP, such as file_get_contents() and file_put_contents() to read/write to RAM.

Other more robust alternatives to consider:

Share This
Posted in: