Reputation: 4710
I know this is a weird question but I have 2 servers, one a vps and another, a mysql host. Now my issue is that I want to try and lower the load on the vps as it is a startup 128mb vps.
Slight problem, what would be required to make the MySQL server work as if it were a cache, I have no access to anything on the MySQL server save the databases im given.
Will this require a separate database? please tell me so if it is possible i can ask my host to add another database.
Thanks!
Upvotes: 2
Views: 461
Reputation: 2500
If you host a website on the VPS that uses MySQL as the database, a typical request goes like this:
There is no way to make a request hit the database without going through the web server first.
If you want to lighten the load on the VPS you can make your script run faster. The most time-consuming part of most web applications is making database queries and waiting for the results. You can, in a lot of cases, avoid that by caching the results of your database queries on the VPS using an in-memory cache like memcached. Be careful with the memory settings though - be sure to set the maximum allocated memory to a sufficiently low setting because your server has very little memory.
Here is a basic example of caching the result of a SELECT query:
$cache = new Memcached();
$cache->addServer('localhost', 11211);
$article_key = "article[$article_id]";
$article = $cache->get($article_key);
if ($article === FALSE) {
$statement = $conn->prepare("SELECT * FROM articles WHERE id = :id");
$statement->exec(array('id' => $article_id));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
if (count($result) === 0) {
// return a 404 status
}
$article = $result[0];
$cache->set($article_key, $article);
}
// display the article
Upvotes: 2