用mysql觸發(fā)器自動(dòng)更新memcache的實(shí)現(xiàn)代碼
mysql 5.1支持觸發(fā)器以及自定義函數(shù)接口(UDF)的特性,如果配合libmemcache以及Memcached Functions for MySQL,就能夠?qū)崿F(xiàn)memcache的自動(dòng)更新。簡(jiǎn)單記錄一下安裝測(cè)試步驟。
安裝步驟
- 安裝memcached,這個(gè)步驟很簡(jiǎn)單,隨處可見
- 安裝mysql server 5.1RC,安裝辦法也很大眾,不廢話了
- 編譯libmemcached,解壓后安裝即可
./configure; make; make install
- 編譯Memcached Functions for MySQL,在http://download.tangent.org/找一個(gè)最新的版本下載就是,
./configure --with-mysql=/usr/local/mysql/bin/mysql_config --libdir=/usr/local/mysql/lib/mysql/
make
make install
- 接下來(lái)有兩個(gè)辦法讓Memcached Functions for MySQL在mysql中生效
- 在mysql的shell中執(zhí)行memcached_functions_mysql源碼目錄下的sql/install_functions.sql,這會(huì)把memcache function作為UDF加入mysql
- 運(yùn)行memcached_functions_mysql源碼目錄下的utils/install.pl,這是一個(gè)perl腳本,作用同上一條
測(cè)試memcache function
以下測(cè)試腳本摘自memcached_functions_mysql的源碼目錄,有興趣可以試試
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');
相關(guān)文章
深入理解PHP原理之Session Gc的一個(gè)小概率Notice
PHP Notice: session_start(): ps_files_cleanup_dir2011-04-04php 過濾英文標(biāo)點(diǎn)符號(hào)及過濾中文標(biāo)點(diǎn)符號(hào)代碼
這篇文章主要介紹了php過濾英文標(biāo)點(diǎn)符號(hào)及過濾中文標(biāo)點(diǎn)符號(hào)的方法,需要的朋友可以參考下2014-06-06php session 寫入數(shù)據(jù)庫(kù)
這篇文章主要介紹了php session 寫入數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2016-02-02簡(jiǎn)介WordPress中用于獲取首頁(yè)和站點(diǎn)鏈接的PHP函數(shù)
這篇文章主要介紹了WordPress中用于獲取首頁(yè)和站點(diǎn)鏈接的PHP函數(shù),分別是home_url()和site_url()需要的朋友可以參考下2015-12-12PHP pthreads v3下同步處理synchronized用法示例
這篇文章主要介紹了PHP pthreads v3下同步處理synchronized用法,結(jié)合實(shí)例形式分析了PHP pthreads v3下同步處理synchronized原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-02-02