利用mysql函数更新memcached

Memcached Functions for MYSQL官方主页:

https://launchpad.net/memcached-udfs

 

两篇基本文章:

http://www.libing.name/2009/02/06/mysql-map-data-to-memcached.html
http://www.libing.name/2008/02/26/mysql-map-memcached.html

 

安装和验证的SQL语句:

http://hg.tangent.org/memcached_functions_mysql/file/724eeccc55bc/README

 

安装:

1.安装mysql

 

2.安装memcached

 

3.启动mysql,memcached

   /usr/local/bin/memcached -p 22222 -m 64m -u root -d

 

4.安装Memcached Functions for MYSQL

tar xzvf memcached_functions_mysql-0.2.tar.gz
cd memcached_functions_mysql-0.2
./configure –with-mysql=MYSQL_DIR/bin/mysql_config –libdir=MYSQL_DIR/lib/mysql/
make
make install

 

5.将UDFs加载到MySQL中(UDF=User Defined Function)

    memc_servers_set()
    mysql> create function memc_servers_set returns int soname "libmemcached_functions_mysql.so";

    memc_set()
    mysql> create function memc_set returns int soname "libmemcached_functions_mysql.so";

    memc_get()
    mysql> create function memc_get returns string soname "libmemcached_functions_mysql.so";

    memc_delete()
    mysql> create function memc_delete returns string soname "libmemcached_functions_mysql.so";

    ......

    其他函数的加载省略

 

6.验证

select memc_servers_set('127.0.0.1:22222');
select memc_set('key','value');
select memc_get('key');
select memc_delete('key');

 

7.触发器更新memcached

(1)创建表

create table memc_test(

    key varchar(100),

    value varchar(100)

);

(2)创建触发器

delimiter ;;

create trigger memc_add after insert on memc_test for each row begin

    set @tmp=memc_servers_set('localhost:11211');

    set @tmp=memc_set(NEW.key,NEW.value);

end;;

delimiter ;

 

delimiter ;;

create trigger memc_update after update on memc_test for each row begin

    set @tmp=memc_servers_set('localhost:11211');

    set @tmp=memc_set(NEW.key,NEW.value);

end;;

delimiter ;

 

delimiter ;;

create trigger memc_del after delete on memc_test for each row begin

    set @tmp=memc_servers_set('localhost:11211');

    set @tmp=memc_delete(OLD.key);

end;;

delimiter ;

 

(3)测试

查询memcached

telnet localhost 22222

get key1

END

没有值

 

向数据库中插入一条

insert into memc_test values('key1','value1');

 

get key1

value1

END

得到值,证明UDFs正常工作了。

你可能感兴趣的:(mysql,function,memcached,delete,insert,each)