不错的一篇文章,用于项目中可以带来更多的便利,按照方法已经调试成功,可以大大提高项目的速度。
FROM:http://www.ooso.net/index.php/archives/436
mysql 5.1支持触发器以及自定义函数接口(UDF)的特性,如果配合libmemcache以及Memcached Functions for MySQL,就能够实现memcache的自动更新。简单记录一下安装测试步骤。
安装步骤
- 安装memcached,这个步骤很简单,随处可见
- 安装mysql server 5.1RC,安装办法也很大众,不废话了
- 编译libmemcached,解压后安装即可
./configure; make; make install
- 编译Memcached Functions for MySQL,在http://download.tangent.org/找一个最新的版本下载就是,
./configure --with-mysql=/usr/local/mysql/bin/mysql_config --libdir=/usr/local/mysql/lib/mysql/
make
make install - 接下来有两个办法让Memcached Functions for MySQL在mysql中生效
- 在mysql的shell中执行memcached_functions_mysql源码目录下的sql/install_functions.sql,这会把memcache function作为UDF加入mysql
- 运行memcached_functions_mysql源码目录下的utils/install.pl,这是一个perl脚本,作用同上一条
测试memcache function
以下测试脚本摘自memcached_functions_mysql的源码目录,有兴趣可以试试
1 drop table if exists urls;
2 create table urls (
3 id int(3) not null,
4 url varchar(64) not null default '',
5 primary key (id)
6 );
7 select memc_servers_set('localhost:11211');
8 select memc_set('urls:sequence', 0);
9 DELIMITER
10 DROP TRIGGER IF EXISTS url_mem_insert;
11 CREATE TRIGGER url_mem_insert
12 BEFORE INSERT ON urls
13 FOR EACH ROW BEGIN
14 SET NEW.id= memc_increment('urls:sequence');
15 SET @mm= memc_set(concat('urls:',NEW.id), NEW.url);
16 END
17 DELIMITER ;
18 insert into urls (url) values ('http://google.com');
19 insert into urls (url) values ('http://www.ooso.net/index.php');
20 insert into urls (url) values ('http://www.ooso.net/');
21 insert into urls (url) values ('http://slashdot.org');
22 insert into urls (url) values ('http://mysql.com');
23 select * from urls;
24 select memc_get('urls:1');
25 select memc_get('urls:2');
26 select memc_get('urls:3');
27 select memc_get('urls:4');
28 select memc_get('urls:5');