Memcached 对话 Google ProtocolBuffers (perl)

看看我这张 web  系统架构图,那就知道  使用统一存储格式的好处了。
 在这种结构中你就单纯使用 java ,或 php 某一种单一语言描述格式,存储你集群交互的数据吗!?使用
ProtocolBuffers  吧, Google 已经有 现成的api来帮你扩展了。
参考: http://blog.youkuaiyun.com/lcj8/archive/2009/02/17/3900157.aspx


     作为  Memcached  就是为集群使用,那么作为在缓存存储的数据我想也应该适用与各种环境,这时候 数据结构协议  Google ProtocolBuffers 
参考: http://code.google.com/apis/protocolbuffers/docs/overview.html
其他语言: http://code.google.com/p/protobuf/wiki/OtherLanguages
(可适用与多种语言,
    javascript 读取缓存中的数据 ,你可以想象下你的 web服务开发 的将来!)
目前学习perl 中 就先上个这的代码:
参考:
http://search.cpan.org/~gariev/Google-ProtocolBuffers-0.08/lib/Google/ProtocolBuffers.pm#___top
# !/bin/perl -w
use  Cache :: Memcached;
use  Google :: ProtocolBuffers;
#开启 memd
my   $memd   =  new Cache :: Memcached{servers  =>  [ ' 10.0.2.15:11211 ' ] };

#声明 缓存 存储 protocol 格式
my   $key = ' test ' ;
Google
:: ProtocolBuffers -> parse( "
  message Person{
    required int32 id           =1;
    required string name        =2;
  }
" , {create_accessors => 1 });

my   $data = Person -> encode({
  id
=> 123 ,
  name
=> ' liukaiyi '
});

#缓存存储
$memd -> add( $key   => $data , 3600 );

# ######################################################################


my  $memd  =  new Cache :: Memcached{servers  =>  [ ' 10.0.2.15:11211 ' ] };
my   $person ;{
  #从缓存中取出
  
$person   =  Person -> decode( $memd -> get( $key ));
}

print   $person -> id , " - " , $person -> name;

结果:

  123-liukaiyi

本文转自博客园刘凯毅的博客,原文链接:Memcached 对话 Google ProtocolBuffers (perl),如需转载请自行联系原博主。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值