用mysql觸發器自動更新memcache的實現代碼

來源:互聯網
上載者:User

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');
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.