sudo apt-get updateAdditionally, you should have MySQL and PHP installed on the virtual server.
sudo apt-get install mysql-server php5-mysql php5 php5-memcache
sudo apt-get install memcached
sudo apt-get install php-pearIf you do not have a compiler on your server, you can download build-essential in order to install memcache:
sudo apt-get install build-essentialFinally use PECL (PHP Extension Community Library) to install memcache:
sudo pecl install memcacheSay yes by pressing enter during the installation when you are asked if you would like to "Enable memcache session handler support? [yes] :”
echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.iniNow you are ready to start using Memcache.
ps aux | grep memcacheAdditionally, you can see the memcache stats by typing:
echo "stats settings" | nc localhost 11211
function get_foo(foo_id)
foo = memcached_get("foo:" . foo_id)
return foo if defined foo
foo = fetch_foo_from_database(foo_id)
memcached_set("foo:" . foo_id, foo)
return foo
end
mysql -u root -p and execute the following commands:use test; grant all on test.* to test@localhost identified by 'testing123'; create table example (id int, name varchar(30)); insert into example values (1, "new_data"); exit;Once you have exited MySQL, create the memcache script file:
nano memtest.phpWe are now going to build up the php script step by step (the entire script will be at the end of the section):
<?php
$meminstance = new Memcache();
$meminstance->pconnect('localhost', 11211);mysql_connect("localhost", "test", "testing123") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());$query = "select id from example where name = 'new_data'"; $querykey = "KEY" . md5($query);
$result = $meminstance->get($querykey);
if (!$result) {
$result = mysql_fetch_array(mysql_query("select id from example where name = 'new_data'")) or die('mysql error');
$meminstance->set($querykey, $result, 0, 600);
print "got result from mysql\n";
return 0;
}
print "got result from memcached\n";
return 0;
?><?php
$meminstance = new Memcache();
$meminstance->pconnect('localhost', 11211);
mysql_connect("localhost", "test", "testing123") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$query = "select id from example where name = 'new_data'";
$querykey = "KEY" . md5($query);
$result = $meminstance->get($querykey);
if (!$result) {
$result = mysql_fetch_array(mysql_query("select id from example where name = 'new_data'")) or die('mysql error');
$meminstance->set($querykey, $result, 0, 600);
print "got result from mysql\n";
return 0;
}
print "got result from memcached\n";
return 0;
?>Running the script on the command line produces the following results:
# php memtest.php got result from mysql # php memtest.php got result from memcached # php memtest.php got result from memcached
Article ID: 168
Created On: Tue, Dec 24, 2013 at 12:39 AM
Last Updated On: Tue, Sep 23, 2025 at 11:20 PM
Authored by: ASPHostServer Administrator [[email protected]]
Online URL: http://faq.asphosthelpdesk.com/article.php?id=168