如何在Ubuntu 12.04上安装和使用Memcache

关于Memcache

Memcache是一个通过缓存服务器信息来加速虚拟专用服务器的系统。该程序允许您分配特定数量的服务器ram,以便在一定时间内缓存最近查询的数据。一旦再次请求数据,memcache通过显示缓存的信息而不是从数据库生成结果来加速检索数据的过程。

建立

本教程中的步骤要求用户具有root权限。你可以看到如何设置了在 基础用户指南 。在开始之前,最好更新apt-get以确保我们下载到VPS的所有软件包都是最新的。
sudo apt-get update
此外,您应该在虚拟服务器上安装MySQL和PHP。
sudo apt-get install mysql-server php5-mysql php5 php5-memcache

安装内存缓存

安装memcache需要几个步骤。 要开始,通过apt-get安装memcached。
sudo apt-get install memcached
下一步是安装php-pear,存储memcache的存储库。
sudo apt-get install php-pear
如果你的服务器上没有编译器,你可以下载build-essential来安装memcache:
sudo apt-get install build-essential
最后使用PECL(PHP Extension Community Library)安装memcache:
sudo pecl install memcache
在安装过程中按Enter键,询问您是否要“启用memcache会话处理程序支持? [yes]:“ 在VPS上完成使用PECL安装memcache后,将memcache添加到memcache.ini中:
echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini
现在您已准备好开始使用Memcache。

确认Memcache并查看统计信息

下载Memcache后,您可以通过搜索它来检查它是否已安装:
ps aux | grep memcache
此外,您可以通过键入以下内容查看内存缓存统计信息:
echo "stats settings" | nc localhost 11211

第三步 - Memcache如何工作

Memcache通过重定向代码,首先尝试在查询服务器的数据库之前从缓存中检索数据。缓存通过保存最近检索的服务器数据达一定量的时间来填充。通过缓存最近请求的信息,未来的查询不必经历从数据库检索信息的较长过程,并且可以通过缓存访问它。 内存缓存页面在其主页上显示此缩写代码,以总结Memcache进程:
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

一个简单的Memcache示例

本节将设置一个简单的php脚本,使用memcache来检索最初在mysql表中找到的单个值。 以下步骤设置了一个mysql用户,可以访问相应的数据库,创建一个表以进行查询,并插入我们将在新的mysql表中测试的一个值。 登录到mysql: mysql -u root -p并执行以下命令:
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;
一旦你退出MySQL,创建memcache脚本文件:
nano memtest.php
我们现在将逐步构建php脚本(整个脚本将在部分的结尾):
  • 首先,使用memcache创建一个新的持久连接,memcache在memcache的默认端口11211上运行。
    &lt?php
    $meminstance = new Memcache();
    $meminstance->pconnect('localhost', 11211);
  • 下一步是使用我们之前创建的用户连接到新的mysql数据库:
    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);
  • 脚本首先在缓存中搜索查询的答案。如果结果不存在,脚本会将问题重新路由到原始数据库。一旦查询被原始数据库应答,脚本使用“set”命令将结果存储在memcache中,这两个命令都保存它,并允许用户指定它应该保留在缓存中的秒数(600)会将其保存在缓存中10分钟)。当我们第一次运行脚本时,它会通知我们数据是从mysql数据库收集的。然而,当它这样做时,它将信息存储在高速缓存中,使得脚本的第二次运行从高速缓存检索它并且让用户知道。在10分钟内,缓存被再次清空,运行脚本将使其再次访问数据库。
    $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;
    
    ?>
总的脚本看起来像这样:
&lt?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 memtest.php 
got result from mysql

# php memtest.php 
got result from memcached

# php memtest.php 
got result from memcached

结论

本教程通过将数据库连接到内存缓存来加速从数据库检索数据。但是,请记住,memcache的强大源于一个事实,它是一个缓存 - 它不是一个数据存储。当使用memcache时,不要指望它替换数据库。因为内存缓存只保存给定密钥的设置时间长度的值,所以您可能不会总是找到需要缓存的信息,在这种情况下,拥有原始服务器数据库是势在必行的。 然而,memcache是一个非常有用的程序,可以做很多提高服务器的效率。
赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏