I wrote this to aid our own database issues with our community, and most of it is caused by our heavy use of persistant AJAX calls, hammering the server to often high loads on local processes (like php-FPM), so I wrote a function without ever googling, and so far so good.
maybe this will help or at least provide some testing grounds.
[CODE=php]
function cached_query(string $query, string $fetchtype) : array
{
global $db;
$memtest = new Memcached();
$memtest->addServer('127.0.0.1', 11211);
$retval = $db->query_params($query);
$result = $retval->$fetchtype();
$querykey = 'KEY' . md5($query);
$memtest->set($querykey, $result);
return $memtest->get($querykey);
}
[/CODE]
Test:
[CODE=php]
cached_query('SELECT avatar, username, id FROM users WHERE user_id IN (1)', 'fetchAll');
[/CODE]
I am still working on return cases on the function, so we can call the function and return true or false, and also allow to read array keys from the get() in the function, not sure how to do that except pushing the array to the function param? maybe some advise on this. anyway try it out!
maybe this will help or at least provide some testing grounds.
[CODE=php]
function cached_query(string $query, string $fetchtype) : array
{
global $db;
$memtest = new Memcached();
$memtest->addServer('127.0.0.1', 11211);
$retval = $db->query_params($query);
$result = $retval->$fetchtype();
$querykey = 'KEY' . md5($query);
$memtest->set($querykey, $result);
return $memtest->get($querykey);
}
[/CODE]
Test:
[CODE=php]
cached_query('SELECT avatar, username, id FROM users WHERE user_id IN (1)', 'fetchAll');
[/CODE]
I am still working on return cases on the function, so we can call the function and return true or false, and also allow to read array keys from the get() in the function, not sure how to do that except pushing the array to the function param? maybe some advise on this. anyway try it out!