| Home » Categories » Multiple Categories |
How To Install and Use Memcache on Ubuntu 12.04 |
|
Article Number: 168 | Rating: Unrated | Last Updated: Tue, Sep 23, 2025 at 11:20 PM
|
About MemcacheMemcache is a system that works to speed up virtual private servers by caching server information. The program allows you to allocate a specific amount of the server ram toward caching recently queried data for a certain amount of time. Once the data is requested again, memcache speeds up the process of retrieving it by displaying the cached information instead of generating the result from the database.SetupThe steps in this tutorial require the user to have root privileges. You can see how to set that up in the Basic Users Tutorial.Before starting off, it’s a good idea to update apt-get to make sure that all of the packages we download to the server are up to date. 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 Install MemcacheInstalling memcache takes several steps.To start, install memcached via apt-get. sudo apt-get install memcached The next step is to install php-pear, the repository that stores memcache. 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] :” Once you have completed the installation of memcache with PECL on the server, add memcached to memcache.ini: echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.iniNow you are ready to start using Memcache. Confirm Memcache and See StatsAfter Memcache is downloaded, you can check that it has been installed by searching for it:ps aux | grep memcacheAdditionally, you can see the memcache stats by typing: echo "stats settings" | nc localhost 11211 How Memcache WorksMemcache works by redirecting code to first attempt to retrieve data from the cache before querying the server’s database. The cache populates by saving recently retrieved server data for a certain amount of time. By caching recently requested information, future queries do not have to go through the longer process of retrieving the information from a database and can, instead, access it through the cache.The memcache page shows this abbreviated code on its homepage to summarize the memcache process: 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
A Simple Memcache ExampleThis section will set up a simple php script to use memcache for retrieving a single value originally found in a mysql table.The following steps set up a mysql user who can access the appropriate database, create a table to query, and insert the one value that we will test in the new mysql table. Log into mysql: 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):
Altogether the script looks like this: <?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 |
Attachments
There are no attachments for this article.
|
How To Create a SSL Certificate on Apache for Debian 7
Viewed 2792 times since Fri, Dec 27, 2013
How To Install Node.js with NVM (Node Version Manager) on Server
Viewed 4308 times since Sun, Dec 29, 2013
How To Install an Upstream Version of Node.js on Ubuntu 12.04
Viewed 3022 times since Sun, Dec 29, 2013
How To Install Nagios On Ubuntu 12.10
Viewed 3008 times since Sat, Jan 4, 2014
Docker Explained: How To Create Docker Containers Running Memcached
Viewed 9021 times since Sat, Jan 4, 2014
How To Install Ruby on Rails on Arch Linux with RVM
Viewed 14343 times since Sun, Dec 29, 2013
Initial Server Setup with Debian 7
Viewed 4086 times since Fri, Dec 27, 2013
How To Create Data Queries in PostgreSQL By Using the Select Command
Viewed 14881 times since Mon, Dec 30, 2013
How To Import and Export Databases and Reset a Root Password in MySQL
Viewed 3282 times since Thu, Dec 26, 2013
What is FTP and How Is It Used?
Viewed 2837 times since Fri, Dec 27, 2013
|
