3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PerlからRedisを利用する.

Last updated at Posted at 2014-09-30

RedisDBモジュールを使ってperlからRedisの操作.

インストール

Redisのインストール(Mac)

brew install redis

Redisの起動

# server起動
$ redis-server
# cliet起動
$ redis-cli

それぞれの起動オプションは--helpを付けて実行のこと.

perlのRedisモジュールインストール

cpanm RedisDB

サンプルコード

基本的な, GET/SETとパイプライン

use Data::Dumper;

use RedisDB;

my $redis = RedisDB->new(
    host => 'localhost', 
    port => 6379
);

# SET/GET
$redis->set('A', 100);
print Dumper $redis->get('A');
# > $VAR1 = '100'

$redis->mset('1', 10, '2', 20, '3', 30);

# send_commandを使ったパイプライン
my $data = [];
for my $key ( qw/1 2 3/ ) {
  # callback関数を渡せる
  $redis->send_command('GET', $key, sub {
    my ($self, $value) = @_;
    push(@$data, $value);
  });
}
# callback関数が実行されるまでblock
$redis->mainloop;

print Dumper $data;
# > $VAR1 = [
#           '10', 
#           '20', 
#           '30', 
#           ];

send_commandは, callbackを渡す以外にもget_all_replies等で値を受け取ることができる.

基本的なRedisのコマンドはラップされているが,
hgetallメソッドの戻り値が配列だったりすることは注意.

参考資料

RedisDBモジュール
http://search.cpan.org/dist/RedisDB/lib/RedisDB.pm

Redisのコマンド一覧
http://redis.io/commands

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?