PerlからMemcachedを使ってみる
前回は、memcached基本設定でmemcachedのインストールのみ
しておいたのでPerlから簡易処理して動作確認してみる。
ファイルに書かれた設定やDBのデータをメモリ上にのせておくことで高速にデータの
取得を行うことが出来るようになります。
Memcachedのサービスを落とすとデータは消えます。何らかの理由でmemcachedが落ちてしまうと
データが全部消えて無くなってしまうので、もし永続的に利用したい場合は、
IO Driveなどを利用すると良いかと思います。
CPANでCache::Memcached::Fastをインストール
Cache::Memcahced::Fast is a Perl client for memcached,
a memory cache daemon (http://www.danga.com/memcached/).
Module core is implemented in C and tries hard to minimize number of system calls
and to avoid any key/value copying for speed. As a result, it has very low CPU consumption.API is largely compatible with Cache::Memcached, original pure Perl client,
most users of the original module may start using this module by installing it
and adding “::Fast” to the old name in their scripts
(see “Compatibility with Cache::Memcached” below for full details).
[root@colinux mem]$ perl -MCPAN -e 'install Cache::Memcached::Fast'
CPAN: Storable loaded ok
Going to read /home/user/.cpan/Metadata
Database was generated on Fri, 10 Aug 2012 08:03:04 GMT
Cache::Memcached::Fast is up to date.
[root@colinux mem]$
[root@colinux mem]# cat memcached_fast.pl #!/usr/bin/perl use strict; #use warnings; use Cache::Memcached::Fast; my $key = "key"; my $cache = Cache::Memcached::Fast->new({servers=>["localhost:11211"]}); $cache->set($key,"This is Memcache test by Perl"); if( my $val = $cache->get($key) ) { print "[ GET DATA FROM CACHE ] ".$val."\n";} $cache->replace($key,"This is 2nd Memcache test by Perl"); if( my $val = $cache->get($key) ) { print "[ GET DATA FROM CACHE ] ".$val."\n";} $cache->delete($key); if( my $val = $cache->get($key) ) { print "[ GET DATA FROM CACHE ] ".$val."\n";} print "[ GET DATA FROM CACHE ] Data is deleted!! Please use flush_all to delete all data.\n"; [root@colinux mem]#
[root@colinux mem]$ ./memcached_fast.pl
[ GET DATA FROM CACHE ] This is Memcache test by Perl
[ GET DATA FROM CACHE ] This is 2nd Memcache test by Perl
[ GET DATA FROM CACHE ] Data is deleted!! Please use flush_all to delete all data.
[root@colinux mem]$
関連リンク
memcached基本設定
参考サイト
Cache::Memcached::Fast
Memcachedのインストール・利用方法
Perl Hackers Hub
memcachedを知り尽くす