看看我这张 web 系统架构图,那就知道
使用统一存储格式的好处了。
在这种结构中你就单纯使用 java ,或 php 某一种单一语言描述格式,存储你集群交互的数据吗!?使用
ProtocolBuffers
吧,
Google 已经有 现成的api来帮你扩展了。
参考:
http://blog.csdn.net/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),如需转载请自行联系原博主。