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

2025-03-07 01:11:44
推荐回答(2个)
回答1:

如何在Ubuntu 12.04上安装和使用Memcache
关于Memcache
Memcache通过缓存服务器信息的方式来加速服务器处理能力的系统。Memcache分配一块服务器上的内存来缓存一段特定时间之内的最近的查询数据。一旦某个数据被再次请求,那么Memcache就直接从内存中返回这个数据,而不是从数据库中,所以这将会加速查询速度。
安装
安装前需要用户具有root权限。开始安装前,首先更新系统。
sudo apt-get update
另外,还需要安装MySQL和PHP。
sudo apt-get install mysql-server php5-mysql php5 php5-memcache
安装Memcache
首先安装Memcache。
sudo apt-get install memcached
接着安装php-pear(PHP扩展和应用库)。
sudo apt-get install php-pear
如果没有安装编译器,需要安装build-essential。
sudo apt-get install build-essential
最后使用PECL(PHP扩展社区库)来安装Memcache。
sudo pecl install memcache
安装完成后,向memcache.ini文件中添加memcached:
echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini
现在可以开始使用Memcache了。
确认Memcache并查看其状态
Memcache运行后,可以通过以下命令查看。
ps aux | grep memcache
另外,还可以查看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 -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
详细研究一下memtest.php的脚本文件。
首先创建与Memcache的长连接,使用Memcache的默认端口,11211。
$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());
接着创建查询,然后设置一个标识特定行为的key。
$query = "select id from example where name = 'new_data'";
$querykey = "KEY" . md5($query);
脚本首先查询缓存,如果结果不存在,则脚本再向原始数据库查询。一旦从原始数据库查询到结果,则脚本在Memcache中存储查询的结果,另外还可以设置缓存的有效期,即表示在这个有效期内,查询结果一直保持在缓存中(有效期一般是600秒)。
当首次运行脚本时,数据将会从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;
?>
以下是整个的脚本代码:
$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

回答2:

关于Memcache
Memcache通过缓存服务器信息的方式来加速服务器处理能力的系统。Memcache分配一块服务器上的内存来缓存一段特定时间之内的最近的查询数据。一旦某个数据被再次请求,那么Memcache就直接从内存中返回这个数据,而不是从数据库中,所以这将会加速查询速度。
安装
安装前需要用户具有root权限。开始安装前,首先更新系统。
sudo apt-get update
另外,还需要安装MySQL和PHP。
sudo apt-get install mysql-server php5-mysql php5 php5-memcache
安装Memcache
首先安装Memcache。
sudo apt-get install memcached
接着安装php-pear(PHP扩展和应用库)。
sudo apt-get install php-pear
如果没有安装编译器,需要安装build-essential。
sudo apt-get install build-essential
最后使用PECL(PHP扩展社区库)来安装Memcache。
sudo pecl install memcache
安装完成后,向memcache.ini文件中添加memcached:
echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini
现在可以开始使用Memcache了。
确认Memcache并查看其状态
Memcache运行后,可以通过以下命令查看。
ps aux | grep memcache
另外,还可以查看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 -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
详细研究一下memtest.php的脚本文件。
首先创建与Memcache的长连接,使用Memcache的默认端口,11211。
$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());
接着创建查询,然后设置一个标识特定行为的key。
$query = "select id from example where name = 'new_data'";
$querykey = "KEY" . md5($query);
脚本首先查询缓存,如果结果不存在,则脚本再向原始数据库查询。一旦从原始数据库查询到结果,则脚本在Memcache中存储查询的结果,另外还可以设置缓存的有效期,即表示在这个有效期内,查询结果一直保持在缓存中(有效期一般是600秒)。
当首次运行脚本时,数据将会从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;
?>
以下是整个的脚本代码:
$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