How to Install and Use Memcache on Ubuntu 12.04

This article describes Ubuntu releases that are no longer supported. If you are currently running a server running Ubuntu 12.04, we strongly recommend that you upgrade or migrate to a supported version of Ubuntu:

  • Upgraded to Ubuntu 14.04.

  • Upgrade from Ubuntu 14.04 to Ubuntu 16.04

  • Migrate server data to a supported version

Reason: Ubuntu 12.04 reached End of Life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See:

This guide may still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using the guide written for the version of Ubuntu you are using. You can use the search function at the top of the page to find newer versions.

About memory cache

Memcache is a system for speeding up virtual private servers by caching server information. The program allows you to allocate a specific amount of server memory to cache recently queried data for a period of time. Once the data is requested again, memcache speeds up the retrieval process by displaying cached information instead of generating results from the database.

set up

The steps in this tutorial require a user with root privileges. You can see how to set it up in the basic user tutorial. Before we start, it's a good idea to update apt-get to make sure all the packages we downloaded to the VPS are up to date.

sudo apt-get update

Also, you should install MySQL and PHP on the virtual server.

sudo apt-get install mysql-server php5-mysql php5 php5-memcache

Install Memcache

Installing memcache requires several steps.

First, install memcached via apt-get.

sudo apt-get install memcached

The next step is to install php-pear, the repository where memcache is stored.

sudo apt-get install memcached

If you don't have a compiler on your server, you can download build-essential to install memcache:

sudo apt-get install build-essential

Finally, use PECL (PHP Extension Community Library) to install memcache:

sudo pecl install memcache

When asked if you want to "Enable memcache session handler support? [Yes]:"

After completing the memcache installation on the VPS using PECL, add memcached to memcache.ini:

echo "extensionu003dmemcache.so" | sudo tee /etc/php5/conf.d/memcache.ini

Now you are ready to start using Memcache.

Confirm Memcache and view statistics

After the Memcache download is complete, you can search to see if it is installed:

P.S. | grep memory cache

Additionally, you can view memcache statistics by typing:

echo "Statistics Settings" | CNC localhost 11211

Step 3 - How Memcache Works

Memcache works by redirecting code to first try to retrieve data from the cache before querying the server's database. Populates the cache by saving recently retrieved server data for a period of time. By caching recently requested information, future queries don't have to go through the lengthy process of retrieving information from the database, but instead can access it through the cache.

The memcache page shows this abbreviated code on its home page to summarize the memcache process:

function get_foo(foo_id)

foo u003d memcached_get("foo:" . foo_id)

If foo is defined, return foo

foo u003d fetch_ foo_ from\database(foo_id)

memcached_set("foo:" . foo_id, foo)

return foo

end

A simple Memcache example

This section will set up a simple php script to use memcache to retrieve a single value originally found in a mysql table.

The following steps set up a mysql user with access to the appropriate database, create a table to query, and insert one of the values ​​we will test into the new mysql table.

Log in to mysql: mysql -u root -pand execute the following command:

use test;

grant all test@localhost identified by "testing123" on test.*;

create_table_example(id int, name varchar(30));

insert_example_value(1, "new_data");

exit;

After exiting MySQL, create a memcache script file:

nano-memtest.php

We will now build the php script step by step (the whole script will be at the end of this section):

  • First create a new persistent connection to memcache running on memcache's default port 11211.
<?php

$meminstance u003d new Memcache();

$meminstance->pconnect('localhost', 11211);

    下一步是使用我们之前创建的用户连接到新的 mysql 数据库:

mysql_connect("localhost", "test", "testing123") 或死(mysql_error());

mysql\select\db("text") 或死(mysql_error());

    之后,继续创建我们将向服务器提出的查询,并提供一个键来识别该特定操作:

$query u003d "从 name u003d 'new_data' 的示例中选择 id";

$querykey u003d "KEY"。 md5($查询);

// 脚本首先在缓存中搜索查询的答案。如果结果不存在,则脚本将问题重新路由到原始数据库。一旦原始数据库回答了查询,脚本将使用“set”命令将结果存储在 memcache 中——既保存它又允许用户指定它应该在缓存中保留的秒数(600会将其保存在缓存中 10 分钟)。

当我们第一次运行脚本时,它会告诉我们数据是从 mysql 数据库中收集的。然而,当它这样做时,它将信息存储在缓存中,以便脚本的第二次运行从缓存中检索它并让用户知道。

10 分钟后缓存再次清空,运行脚本将使其再次访问数据库。

$result u003d $meminstance->get($querykey);

如果(!$结果){

$result u003d mysql_fetch_array(mysql_query("select id from example where name u003d 'new_data'")) or die('mysql error');

$meminstance->set($querykey, $result, 0, 600);

print "从mysql得到结果\n";

返回0;

}

print "从内存缓存中得到结果\n";

返回0;

?>

The whole script looks like this:
 

<?php

$meminstance u003d new Memcache();

$meminstance->pconnect('localhost', 11211);

mysql_connect("localhost", "test", "testing123") 或死(mysql_error());

mysql\select\db("text") 或死(mysql_error());

$query u003d "从 name u003d 'new_data' 的示例中选择 id";

$querykey u003d "KEY"。 md5($查询);

$result u003d $meminstance->get($querykey);

如果(!$结果){

$result u003d mysql_fetch_array(mysql_query("select id from example where name u003d 'new_data'")) or die('mysql error');

$meminstance->set($querykey, $result, 0, 600);

print "从mysql得到结果\n";

返回0;

}

print "从内存缓存中得到结果\n";

返回0;

?>

Running the script on the command line produces the following results:

# php memtest.php

get result from mysql

php memtest.php

Get results from memcached

php memtest.php

Get results from memcached

in conclusion

This tutorial describes how to speed up data retrieval from a database by connecting it to an in-memory cache. However, keep in mind that memcache's strength stems from the fact that it's a cache -- it's not a data store. When using memcache, don't expect it to replace your database. Because memcache only holds the value for a given key for a set length of time, you may not always be able to find the information you need to cache, in which case it is necessary to have the origin server database.

Nonetheless, memcache is a very useful program that can greatly improve server efficiency.

Guess you like

Origin blog.csdn.net/zhishifufei/article/details/127670723