用mysql触发器自动更新memcache的实现代码

yipeiwu_com5年前Mysql基础

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的源码目录,有兴趣可以试试

drop table if exists urls;
create table urls (
 
id int(3) not null,
 
url varchar(64) not null default '',
 
primary key (id)
 
);
select memc_servers_set('localhost:11211');
select memc_set('urls:sequence', 0);
DELIMITER
DROP TRIGGER IF EXISTS url_mem_insert;
CREATE TRIGGER url_mem_insert
BEFORE INSERT ON urls
FOR EACH ROW BEGIN
    
SET NEW.id= memc_increment('urls:sequence');
    
SET @mm= memc_set(concat('urls:',NEW.id), NEW.url);
END
DELIMITER ;
insert into urls (url) values ('http://google.com');
insert into urls (url) values ('http://www.ooso.net/index.php');
insert into urls (url) values ('http://www.ooso.net/');
insert into urls (url) values ('http://slashdot.org');
insert into urls (url) values ('http://mysql.com');
select * from urls;
select memc_get('urls:1');
select memc_get('urls:2');
select memc_get('urls:3');
select memc_get('urls:4');
select memc_get('urls:5');

相关文章

php 随机记录mysql rand()造成CPU 100%的解决办法

百度查阅了一些资料,再结合自己的一些经验,采用以下解决办法: 复制代码 代码如下: $idlist=''; for($i=1;$i<=20;$i++){ if($i==1){ $i...

使用Limit参数优化MySQL查询的方法

前些天看了一个老外写的程序,在 MySQL 查询中使用了很多 Limit 关键字,这就让我很感兴趣了,因为在我印象中, Limit 关键字似乎更多被使用 MySQL 数据库的程序员用来做...

PHP+Mysql+jQuery查询和列表框选择操作实例讲解

PHP+Mysql+jQuery查询和列表框选择操作实例讲解

本文讲解如何通过ajax查询mysql数据,并将返回的数据显示在待选列表中,再通过选择最终将选项加入到已选区,可以用在许多后台管理系统中。本文列表框的操作依赖jquery插件。 HTM...

PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能

PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能

本文实例讲述了PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能。分享给大家供大家参考,具体如下: 一、代码 td{ font-size:9pt; } .s...

apache+php+mysql安装配置方法小结

apache+php+mysql安装配置方法小结

整个安装流程如下: 1,首先安装apache:我安装的版本是: httpd-2.2.16-win32-x86-openssl-0.9.8o.msi 网址:http://www.apach...