0

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;

Omid Navy
  • 272
  • 2
  • 12
  • 1
    Forgive me, but it's not clear to me why you need to use a cache of anything there. If you're just doing inserts, why do you need to select huge amounts of data each time? Maybe if you explain more about the table structure I'll understand better. – jaswrks Nov 08 '17 at 12:00
  • @jaswrks let me explain with an example: in 'mac' table, I have 2million data , ID and value , eg. ID = 100100, value = '54:52:00:27:e4:91' ; In my main table i need to insert the row , so i need the 'mac ID for '54:52:00:27:e4:91' whichis 100100' . I can do this by a Insert Select query which perform very bad , or do it by some kind of cache system. and please correct me if u think its not a good way. – Omid Navy Nov 08 '17 at 12:36

2 Answers2

2

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.

<?php
$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:

jaswrks
  • 1,255
  • 10
  • 13
1

You can use PHP Super Cache which is very simple which is faster than Reddis, Memcache etc

require __DIR__.'/vendor/autoload.php';

use SuperCache\SuperCache as sCache;

//Saving cache value with a key
// sCache::cache('<key>')->set('<value>');
sCache::cache('myKey')->set('Key_value');

//Retrieving cache value with a key
echo sCache::cache('myKey')->get();

https://packagist.org/packages/smart-php/super-cache

shabeer
  • 1,064
  • 9
  • 17