In my prevIoUs post,I listed down most common with sample execution terminal logs. Today I want to discuss about the Memcached Client program available in Java language.
There are three most widely used memcached client programs in java
- xmemcached
- spymemcached
- gwhalin memcached client
I have used Greg Whalin memcached client and found it easy to understand and use. It provides all the basic functionalities with thread pooling. Its available under BSD license and you can download it from below URL:
Once you have downloaded the source code you can create a java project and copy all the java classes and then use it.
To help you get started quickly,I am providing a sample program to showcase the usage of basic functions that can be performed with memcached server.
<span style="color: #0000ff;">import<span style="color: #000000;"> com.meetup.memcached.MemcachedClient;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.meetup.memcached.sockIOPool;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> MemcachedJavaClient {
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* MemcachedJavaClient program to show the usage of different functions
* that can be performed on Memcached server with Java Client
* </span><span style="color: #808080;">@p<a href="/tag/ara/" target="_blank" class="keywords">ara</a>m</span><span style="color: #008000;"> args
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
</span><span style="color: #008000;">//</span><span style="color: #008000;">initialize the SockIOPool that maintains the Memcached Server Connection Pool</span>
String[] servers = {"localhost:11111"<span style="color: #000000;">};
SockIOPool pool </span>= SockIOPool.getInstance("Test1"<span style="color: #000000;">);
pool.setServers( servers );
pool.setFai<a href="/tag/lov/" target="_blank" class="keywords">lov</a>er( </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> );
pool.se<a href="/tag/tini/" target="_blank" class="keywords">tini</a>tConn( </span>10<span style="color: #000000;"> );
pool.setMinConn( </span>5<span style="color: #000000;"> );
pool.setMaxConn( </span>250<span style="color: #000000;"> );
pool.setMaintSleep( </span>30<span style="color: #000000;"> );
pool.setNagle( </span><span style="color: #0000ff;">false</span><span style="color: #000000;"> );
pool.setSocketTO( </span>3000<span style="color: #000000;"> );
pool.setAliveCheck( </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> );
pool.initialize();
</span><span style="color: #008000;">//</span><span style="color: #008000;">Get the Memcached Client from SockIOPool named Test1</span>
MemcachedClient mcc = <span style="color: #0000ff;">new</span> MemcachedClient("Test1"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">add some value in cache</span>
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("add status:"+mcc.add("1","Original"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">Get value from cache</span>
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("Get from Cache:"+mcc.get("1"<span style="color: #000000;">));
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"add status:"+mcc.add("1","Modified"<span style="color: #000000;">));
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"Get from Cache:"+mcc.get("1"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">use set function to add/update value,use replace to update and not add</span>
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("set status:"+mcc.set("1","Modified"<span style="color: #000000;">));
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"Get from Cache after set:"+mcc.get("1"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">use delete function to delete key from cache</span>
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("remove status:"+mcc.delete("1"<span style="color: #000000;">));
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"Get from Cache after delete:"+mcc.get("1"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">Use getMulti function to retrieve multiple keys values in one function
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Its helpful in reducing network calls to 1</span>
mcc.set("2","2"<span style="color: #000000;">);
mcc.set(</span>"3","3"<span style="color: #000000;">);
mcc.set(</span>"4","4"<span style="color: #000000;">);
mcc.set(</span>"5","5"<span style="color: #000000;">);
String [] keys </span>= {"1","2","3","INVALID","5"<span style="color: #000000;">};
HashMap</span><String,Object> hm = (HashMap<String,Object><span style="color: #000000;">) mcc.getMulti(keys);
</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(String key : hm.keySet()){
Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"KEY:"+key+" VALUE:"+<span style="color: #000000;">hm.get(key));
}
}
}
Output of the above program is:
rush: bash; title: ; notranslate">add status:true Get from Cache:Original add status:false Get from Cache:Original set status:true Get from Cache after set:Modified remove status:true Get from Cache after delete:null KEY:3 VALUE:3 KEY:2 VALUE:2 KEY:1 VALUE:null KEY:INVALID VALUE:null KEY:5 VALUE:5
If you want to connect to multiple memcached servers then you will have to create multiple SockIOPool instances and then use the same name while getting the MemcacheClient instance.
reference:http://www.journaldev.com/24/memcached-java-client-with-sample-program
补充:memcachedclient获取方式有3中,上面是一种:
MemcachedClient memcacheClient=<span style="color: #0000ff;">new<span style="color: #000000;"> MemcachedClient(
<span style="color: #0000ff;">new InetSocketAddress("127.0.0.1:11211",11211));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。